我一直在通過網上找教程來學習python,但我似乎無法弄清楚為什么這段代碼不適用于我正在制作的這個“游戲”。我希望玩家能夠看到串列中有哪些物品,并且我希望他們能夠通過輸入他們想要拿走的東西從串列中拿走東西。我遇到的問題是當你拿一個物品時游戲顯示錯誤的文本行。它不會像它應該的那樣說“你拿 a”,而是列印“無效命令”行,但它仍然將“a”添加到玩家的庫存中。我一生都無法弄清楚為什么它仍在添加專案而不列印正確的文本。
inventory=[]
closet=['A','B','C']
Closetloop=False
while Closetloop==False:
print('Inside Your closet You have:')
sleep(1)
print(closet)
sleep(2)
print('What items do you take out? (Type "exit" to exit closet))
ClosetTake=input('You take:')
sleep(1)
if ClosetTake.lower()=='a':
if 'A' in closet:
os.system('cls')
print('You take the a')
res = inventory.insert(0, closet.pop(closet.index('A')))
Closetloop=False
else:
os.system('cls')
print('Invalid command')
Closetloop=False
if ClosetTake.lower()=='b':
if 'B' in closet:
os.system('cls')
print('You take the b')
res = inventory.insert(0, closet.pop(closet.index('B')))
Closetloop=False
else:
os.system('cls')
print('Invalid command')
Closetloop=False
if ClosetTake.lower()=='c':
if 'C' in closet:
os.system('cls')
print('You take the c')
res = inventory.insert(0, closet.pop(closet.index('C')))
Closetloop=False
else:
os.system('cls')
print('Invalid command')
Closetloop=False
if ClosetTake.lower()=='exit':
os.system('cls')
print('You exit the closet')
Closetloop=True
uj5u.com熱心網友回復:
我重寫了代碼以展示其他一些可能感興趣的 Python 特性。
inventory=set()
closet ={'A','B','C'}
while True:
if closet:
print(closet)
closet_take = input('Which item to take else \"exit\" to leave ').upper()
if closet_take == 'EXIT':
print(inventory)
print('bye')
break
if closet_take in closet:
closet.remove(closet_take)
inventory.add(closet_take)
continue
else:
print('Invalid Entry')
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/512662.html
上一篇:如何在c中縮短我的if陳述句?
