lundi 15 août 2022

How do I get a Different Random Choice When People Decided to Play Again? Python

I'm basically trying to get a different enemy every single time that people play/runs the code. For now, If I decided to play again the code keeps using the same values as before and I really want them to be different. Please help me. I'm New to using Python!

I left you the whole code of the program in case It helps you to help me. Thanks! I hope I will help others soon.

import random
items = []
enemy = random.choice(["Troll", "Pirate", "Gorgon", "Monster"])
weapons = random.choice(["Bow", "Axe", "Bazooka", "Sword"])


def print_pause(message_to_print):
    print(message_to_print)
    time.sleep(1)


def intro():
    print_pause("You find yourself standing in an open field, "
                "filled with grass and yellow wildflowers.")
    print_pause(f"Rumor has it that a {enemy} is somewhere around here, and "
                "has been terrifying the nearby village.")


def choice():
    while True:
        choice = input("Enter 1 to knock on the door of the house.\n"
                       "Enter 2 to peer into the cave.\n"
                       "What would you like to do?\n"
                       "Please enter 1 or 2.\n")
        if choice == '1':
            house()
        elif choice == '2':
            cave()
        else:
            print("Please enter 1 or 2.\n")


def fight():
    print_pause(f"The {enemy} attacks you!")
    fight = input("Would you like to (1) fight or (2) run away?\n")
    if fight == '1':
        if ({weapons}) in items:
            print_pause(f"As the {enemy} moves to attack, you unsheath "
                        f"your new {weapons}.")
            print_pause(f"The {weapons} of Ogoroth shines brightly in your "
                        "hand as you brace yourself for the attack.")
            print_pause(f"But the {enemy} takes one look at your shiny new "
                        "toy and runs away!")
            print_pause(f"You have rid the town of the {enemy}. "
                        "You are victorious!")
            play_again()
        else:
            print_pause(f"You feel a bit under-prepared for this, "
                        "what with only having a tiny dagger.")
            print_pause(f"You do your best...")
            print_pause(f"but your dagger is no match for the {enemy}.")
            print_pause(f"You have been defeated!")
            print_pause(f"G A M E   O V E R")
            play_again()

    elif fight == '2':
        print_pause("You run back into the field. Luckily, "
                    "you don't seem to have been followed.")
        choice()


def house():
    print_pause("You approach the door of the house.")
    print_pause(f"You are about to knock when the door "
                f"opens and out steps a {enemy}.")
    print_pause(f"Eep! This is the {enemy} house!")
    fight()


def cave():
    if ({weapons}) in items:
        print_pause("You've been here before, and gotten all "
                    "the good stuff. It's just an empty cave now.")
        print_pause("You walk back out to the field.")

    else:
        print_pause(f"You peer cautiously into the cave.")
        print_pause(f"It turns out to be only a very small cave.")
        print_pause(f"Your eye catches a glint of metal behind a rock.")
        print_pause(f"You have found the magical {weapons} of Ogoroth!")
        print_pause(f"You discard your silly old dagger and take "
                    f"the {weapons} with you.")
        print_pause(f"You walk back out to the field.")
        items.append({weapons})
    choice()


def play_again():
    while True:
        choice = input("Play again? [y|n]\n")
        if choice == 'y':
            play_game()
        elif choice == 'n':
            print('Thanks for playing! Goodbye!')
            exit(0)
        else:
            print("(Please enter y or n.")


def play_game():
    intro()
    choice()

play_game()



Aucun commentaire:

Enregistrer un commentaire