0

This is a for a text based game.

I have a class Room with a function called choose(self,room,choice)

Every other room is a subclass, also with a choose() function, and I have it set up so that individual rooms run their own code from the user's input, but universal ones, like checking inventory and printing a list of commands go through the superclass, so each room looks like this

class LivingRoom:
    def __init__(self):
        ...
        ...

    def show(self):
        ...
        ...

    def choose(self,choice):  #'choice' is whatever the play inputs
            if "help" or "inventory" or "take" or "drop" or "look" or "attack" in choice:
                Room.choose(Room,self,choice) #It pass the current room and the player's 
                                              #input into the function

            elif "north" in choice:
                ...
                ...
            elif "west in choice:
                ...
                ...
            else:
                print("I don't know what you want to do.")

The problem I'm having is that, no matter what the player's input is, it always runs the first IF statement and goes to the Room.choose function.

I tested it out by taking out all of the or statements and it worked fine, so for some reason, having or statements is making that if statement always run.

When I put all of the choices in parenthesis like so

if ("help" or "inventory" or "take" or "drop" or "look" or "attack") in choice:
                Room.choose(Room,self,choice)

It fixed the problem, so that it only ran the if statement when I wanted, but any of the options that referenced the current room, like "take", "drop", "look", and "attack" (all of those reference the current room to check that whatever item or enemy the player wants to interact with is in that room) didn't run. "help and "inventory" work fine because they don't reference the current room.

If it's helpful, I'll post the Room.choose() function in its entirety.

def choose(self,room,choice):
    print("        \n{{{ROOM FUNCTION RUNNING.}}}") #I put this in so I could debug and it's what showed my that my IF statement was running constantly
    if "quit" in choice or choice == "q":
        run = False
    elif "help" in choice:
        print("          [ COMMANDS ]")
        print("'NORTH', 'SOUTH', 'EAST', 'WEST': move in that direction.\n")
        print("'UP', 'down': move up or down a staircase,ladder,etc.\n")
        print("'TAKE' or 'PICK UP' places an item in the room in your inventory. 'DROP' places an item from your inventory into a room.\n")
        print("'I' or 'INVENTORY' displays your currently held items\n.")
        print("'LOOK' replays a room description.\n")
        print("'HIT' or 'ATTACK' to attack enemies.")

    elif "inventory" in choice or choice == "i":
        if P.inventory:
            for i in P.inventory:
                print("[  " + i.name + "  ]")
        elif not P.inventory:
            print("")
            print("Your aren't carrying anything.")
    elif "intro" in choice:
        showIntro()
    elif "look" in choice:
        room.show()
    elif "take" in choice:
        counter = 0
        for i in room.items:
            if i.name.lower() in choice:
                counter = 1
                P.inventory.append(i)
                room.items.remove(i)
                print("\nYou took a " + i.name.lower())
        if counter == 0:
            print("\nThere isn't anything like that here.\n")
    elif "drop" in choice:
        counter = 0
        for i in P.inventory:
            if i.name.lower() in choice:
                counter = 1
                P.inventory.remove(i)
                room.items.append(i)
                print("\nYou dropped a " + i.name.lower() )
        if counter == 0:
            print("\nYou're not carrying an item like that.") 
        for i in room.items:
            if i.name.lower() in choice:
                print("\n" + i.examine)
        for i in P.inventory:
            if i.name.lower() in choice:
                print("\n" + i.examine)
Z Taylor
  • 7
  • 3
  • So, I looked at the duplicate question, thanks for that. Does that mean I just need to put `if "help" in choice or "inventory" in choice ....` etc. ? – Z Taylor Nov 06 '17 at 17:12
  • Do you mean using a set? Like, saying `if choice in {"help","inventory","take", etc.}` If so, will that work if `choice` contains other words? Like, if the player inputs "look at sword" or something, my understanding would be that, for `choice` to be IN that set, the set would need to contain the phrase "look at sword", unless I'm missing something. – Z Taylor Nov 06 '17 at 17:20
  • Yes, your understanding is correct. `if choice in {"help","inventory","take", etc.}` won't work in this case -- sorry. You could do `any(x in choice for x in ["help","inventory","take", etc.])` or something similar – vaultah Nov 06 '17 at 17:29
  • Ah, I see. That makes a lot of sense, thanks. I'll change that. – Z Taylor Nov 06 '17 at 17:35
  • If choice is always such that the action word is first in a possibly whitespace separated string then do something like if choice.split()[0] in ('help', 'inventory', 'take', ): I would instead have the choice words mapped to functions by a dict. Then you could do action = choice_map.get(choice.split()[0]) if action: action(choice) – Samantha Atkins Nov 06 '17 at 17:36

0 Answers0