Python 3 - IF中的OR语句使其始终运行

时间:2017-11-06 17:07:33

标签: python python-3.x if-statement

这是一款基于文字的游戏。

我的课程Room有一个名为choose(self,room,choice)

的函数

每个其他房间都是一个子类,也有一个choose()功能,我设置它,以便各个房间从用户输入运行自己的代码,但是通用的代码,如检查库存和打印清单命令通过超类,所以每个房间都是这样的

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.")

我遇到的问题是,无论玩家的输入是什么,它总是运行第一个IF语句并转到Room.choose函数。

我通过取出所有or语句来测试它并且它工作正常,因此出于某种原因,使用or语句使if语句始终运行。

当我把所有选择都放在括号中时如此

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

它修复了问题,因此它只在我想要的时候运行if语句,但引用当前房间的任何选项,如“take”,“drop”,“look”和“攻击“(所有这些引用当前房间来检查玩家想要与之交互的任何物品或敌人是否在那个房间内)没有运行。 “帮助和”库存“工作正常,因为他们没有引用当前的房间。

如果它有用,我会完整地发布Room.choose()功能。

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)

0 个答案:

没有答案