python or语句使用_Python 3 - IF中的OR语句使其始终运行

这是一个基于文本的游戏。

我有一个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”和“attack”(所有这些引用当前房间检查玩家想要与之互动的任何物品或敌人在该房间中)没有运行。

“帮助和”库存“工作正常,因为他们没有参考当前房间。

如果有帮助,我会完整发布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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值