dimanche 20 novembre 2022

Setting a conditional for a random item selected from a list in Python

I'm working on a text-based Choose Your Own Adventure game in Python for an online class. The game has a list of random "villains" that you may encounter. The original project just has you going to the cave and finding a magical sword that you use to fight the villain. I wanted to set it so that the "weapon" would change according to whatever villian is selected randomly for the list. I listed the code I came up with (below), but it is not recognizing the choice of creature. Instead, it is returning the sword each time. What am I doing wrong?

import random
import time

creatures = ["wicked fairy", "gorgon", "troll", "dragon", "small child", "Karen", "ex-wife"]
weapons = ["Sword of Ogoroth", "Nintendo Switch", "social media", "alimony"]
creature = random.choice(creatures)
items = []

def print_pause(message):
    print(message)
    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 {creature} is somewhere around here, and has been terrifying the nearby village.")
    print_pause("In front of you is a house.")
    print_pause("To your right is a dark cave.")
    print_pause("In your hand, you hold your trusty (but not very effective) dagger.")

def house_or_cave_choice():
    print_pause("Enter 1 to knock on the door of the house.")
    print_pause("Enter 2 to peer into the cave.")
    print_pause("What would you like to do?")
    user_choice = input("(Please enter 1 or 2.)\n")
    if user_choice == "1":
        house()
        house_choice()
    elif user_choice == "2":
        if "sword" in items:
            #if I go back into the cave a second time
            print_pause("You peer cautiously into the cave.")
            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.")
            house_or_cave_choice()
        else: 
            cave()
            house_or_cave_choice()
    else:
        house_or_cave_choice()

def house():
    print_pause("You approach the door of the house.")
    print_pause(f"You are about to knock when the door opens and out steps a {creature}.")
    print_pause(f"Eep! This is the {creature}'s house!")
    print_pause(f"The {creature} attacks you!")
    if "sword" or "Switch" or "phone" or "money" not in items:
        print_pause("You feel a bit under-prepared for this, what with only having a tiny dagger.")
        
    
def house_choice():
    fight_or_run = input("Would you like to (1) fight or (2) run away?\n")
    if fight_or_run == "1":
        if "sword" in items:
            #if user chooses to fight and already has gone to the cave and gotten the weapon
            print_pause(f"As the {creature} moves to attack, you unsheath your new sword.")
            print_pause("The Sword of Ogoroth shines brightly in your hand as you brace yourself for the attack.")
            print_pause(f"But the {creature} takes one look at your shiny new toy and runs away.")
            print_pause(f"You have rid the town of the {creature}. You are victorious!")
            play_again()
        elif "Switch" in items:
            #if user chooses to fight and already has gone to the cave and gotten the weapon
            print_pause(f"As the {creature} moves to attack, you pull out your new Nintendo Switch.")
            print_pause("The Switch shines brightly in your hand, and the game playing on the screen plays distracting sounds.")
            print_pause(f"The {creature} grabs your shiny new toy and runs away.")
            print_pause(f"You have rid the town of the {creature}. You are victorious!")
            play_again()
        elif "phone" in items:
            #if user chooses to fight and already has gone to the cave and gotten the weapon
            print_pause(f"As the {creature} moves to attack, you hold up your new phone, which is already live streaming.")
            print_pause("The phone is recording every word she is about to say.")
            print_pause(f"{creature} takes one look at phone and runs away.")
            print_pause(f"You have rid the town of the {creature}. You are victorious!")
            play_again()
        if "money" in items:
            #if user chooses to fight and already has gone to the cave and gotten the weapon
            print_pause(f"As the {creature} moves to attack, you pull out your fresh stack of cash.")
            print_pause("You have the money for your overdue alimony.")
            print_pause(f"Your {creature} grabs the cash and runs away.")
            print_pause(f"You have rid the town of the {creature} -- at least for this month. You are victorious!")
            play_again()
        else:
            #if user chooses to fight without having gone to the cave first
            print_pause("You do your best...")
            print_pause(f"But your dagger is no match for the {creature}.")
            print_pause("You have been defeated!")
            play_again()
    elif fight_or_run == "2":
        #if user chooses to run away
        print_pause("You run back into the field. Luckily, you don't seem to have been followed.")
        house_or_cave_choice()
    else: 
        #if user puts in something other than a '1' or '2'
        house_choice()
        

def play_again():
    play_again = input("Would you like to play again? (y/n)\n").lower
    if play_again == "y":
        #if user chooses to play again
        "Excellent! Restarting the game..."
        game()
    elif play_again == "n":
        #if user chooses not to play
        "Thanks for playing! See you next time."
        
    else:
        #if user chooses something other than 'y' or 'n'
        play_again()

def cave():
    print_pause("You peer cautiously into the cave.")
    print_pause("It turns out to be only a very small cave.")
    print_pause("Your eye catches a glint of something behind a rock.")
    if {creature} == "wicked fairy" or "gorgon" or "troll" or "dragon":
        print_pause("You find the magical Sword of Ogoroth!")
        print_pause("You discard your silly old dagger and take the sword with you.")
        print_pause("You walk back out to the field.")
        items.append("sword")
    elif {creature} == "small child":
        print_pause("You find a shiny new Nintendo Switch.")
        print_pause("You discard your silly old dagger and take the Switch with you.")
        print_pause("You walk back out to the field.")
        items.append("Switch")
    elif {creature} == "Karen":
        print_pause("You find a shiny new iPhone with a live stream already open on it.")
        print_pause("You discard your silly old dagger and take the phone with you, live streaming as you go.")
        print_pause("You walk back out to the field.")
        items.append("phone")
    else: 
        print_pause("You find a stack of fresh cash.")
        print_pause("You discard your silly old dagger and take the money with you.")
        print_pause("You walk back out to the field.")
        items.append("money")
    
def game():
    intro()
    house_or_cave_choice()
    
game()    




I tried using random choice and conditional statements.




Aucun commentaire:

Enregistrer un commentaire