在下面的代碼中,我試圖檢查一個值(b)是否是kwargs中的一個鍵,如果是,則執行其餘部分。在while循環中使用kwargs - Python 3
def shop(**kwargs):
buy = 1
print ("Welcome to the shop!")
for i, v in kwargs.items():
print (" ", i, ": ", v)
while buy == 1:
b = input ("What would you like to buy? ").lower()
if b == i.lower():
if v <= Player.gold:
Player.gold -= v
Player.inv_plus(i)
print ("You bought ", i, "for ", v, "gold!")
print ("Your gold: ", Player.gold)
print (Player.show_inv())
print()
else:
print ("You don't have enough gold!")
print()
elif b == "exit":
buy = 0
else:
print ("We don't sell that item!")
print()
shop(Stone=5, Potion=10)
但是,當我嘗試運行代碼時,它總是隻允許一個選項。我發現很難解釋,所以我會舉一個例子:
Welcome to the shop!
Stone : 5
Potion : 10
What would you like to buy? stone
We don't sell that item!
What would you like to buy? potion
You bought Potion for 10 gold!
Your gold: 0
Inventory:
Potion 6
Stone 2
它不會接受石,即使它在字典中,但是,它將接受藥水。其他時候,這將是另一種方式。
起初我以爲是因爲它是在一個while循環中,但現在我不太確定,而且我找不到任何可以幫助我在其他地方找到的東西。
對不起,如果這是非常具體的,但這給我很多麻煩。
「的'i'變量結束了在kwargs抱着最後項目的名字」 - 哪裏有什麼結束了最後的到來完全是任意的,而不是必須連接到調用中的參數順序,字母順序或以任何方式一致的任何內容。 – user2357112