【发布时间】:2021-12-18 07:51:26
【问题描述】:
这是我正在尝试构建的一个简单示例,我不知道为什么“钱包”和“戒指”的组合可以作为胜利传递。
我希望玩家获得所有三个物品才能获胜。您对此提出了更好的解决方案吗?
import sys
inventory = []
def addtoInventory(item):
inventory.append(item)
def room():
print('you are in a living room, you say a card. Pick it up?')
pickup = input()
while pickup:
if pickup == 'yes':
addtoInventory("card")
secondroom()
else:
print("you didnt pick it up")
secondroom()
def secondroom():
print('you are now in bedroom, theres a wallet, pick it up?')
pickup = input()
while pickup:
if pickup == 'yes':
addtoInventory("wallet")
bathroom()
else:
print("you didnt pick it up")
bathroom()
def bathroom():
print('you are now in bathroom, theres a ring, pick it up?')
pickup = input()
while pickup:
if pickup == 'yes':
addtoInventory('ring')
mall()
else:
print("you didnt pick it up")
mall()
def mall():
endgame = ["wallet", "card", "ring"]
print('you are about to get a taxi to the pawn shop. do you have everything you need?')
answer = input()
print(inventory)
while answer:
if answer == 'yes':
for item in endgame:
if item not in inventory:
print("you lose")
sys.exit()
else:
print("you won")
sys.exit()
else:
print("you lose")
break
【问题讨论】:
-
那些
while循环在做什么?此外,您可能希望将print("you won")置于for item in endgame:循环之外。 -
您应该在决定是否获胜之前检查您是否拥有所有物品。目前,您检查第一个项目并在那里做出决定,然后是输赢。
'wallet'是第一项,它的存在才是最重要的。您应该改为检查set(inventory) == set(endgame)。
标签: python if-statement inventory text-based