作為我班級的最高任務,我們的任務是撰寫一個旨在在游樂園內玩的游戲。完成游戲后,我們需要讓玩家有機會購買一定數量的 6 種不同的獎品,只要他們有足夠的積分即可購買。
在我的程式中,我做了這樣的設定,如果有人訂購了超過他們平均分數的一定數量的獎品,它會告訴你你沒有足夠的分數來支付它,你需要重新輸入你的答案。
但是,我犯了一個錯誤,即使發送到程式中的數字小于所說的余額,玩家的余額(他們所有積分的總和)也會低于零。我認為這與每個獎品的成本有關,因為其中一個,木蛇,價值 35 分,其他人也可以這樣說,盡管數字不同。
balance = 100
#How many glowsticks the player wants
prize1 = int(input("Tell me again how many glowsticks you want "))
if prize1 <= balance:
prize1 = prize1 * 10
balance = balance - prize1
print("Your balance is now",balance,"points.")
elif prize1 > balance:
print("Too many glowsticks, please change your amount")
break
#Confirm again if that's how many jumbo glasses the player wanted
prize2 = int(input("Tell me again how many jumbo glasses you want "))
if prize2 <= balance:
prize2 = prize2 * 15
balance = balance - prize2
print("Your balance is now",balance,"points.")
elif prize2 > balance:
print("Too many jumbo glasses, please change your amount.")
break
#How many inflatable hammers the player wants
prize1 = int(input("Tell me again how many inflatable hammers you want "))
if prize1 <= balance:
prize1 = prize1 * 25
balance = balance - prize1
print("Your balance is now",balance,"points.")
elif prize1 > balance:
print("Too many inflatable hammers, please change your amount")
break
#How many of dinosaur grabbers the player wants
prize2 = int(input("Tell me again how many dinosaur grabbers you want "))
if prize2 <= balance:
prize2 = prize2 * 30
balance = balance - prize2
print("Your balance is now",balance,"points.")
elif prize2 > balance:
print("Too many dinosaur grabbers, please change your amount.")
break
#How many wooden snakes the player wants
prize1 = int(input("Tell me again how many wooden snakes you want "))
if prize1 <= balance:
prize1 = prize1 * 35
balance = balance - prize1
print("Your balance is now",balance,"points.")
elif prize1 > balance:
print("Too many wooden snakes, please change your amount")
break
#How many foam swords the player wants
prize2 = int(input("Tell me again how many foam swords you want "))
if prize2 <= balance:
prize2 = prize2 * 40
balance = balance - prize2
print("Your balance is now",balance,"points.")
elif prize2 > balance:
print("Too many foam swords, please change your amount.")
break
這是我的代碼發生問題的部分。我想要它,以便玩家一旦獲勝,就可以輸入他們想要的每個獎品的數量。如果他們負擔不起他們想要的金額,因為這會導致您的余額低于零,或者超過您的余額,程式會要求您更改金額以適應它。
PS 本例中余額為 100 的原因是為了演示目的。它可能并不總是等于該數字,并且可能更高或更低,具體取決于玩家在整個游戲中的表現。
uj5u.com熱心網友回復:
編程中有一條關鍵規則叫做 DRY:不要重復自己。注意這個架構是如何用更少的代碼解決這個問題的。它可以輕松添加新專案,如果您想出更好的處理方式,一個修復程式適用于所有情況:
balance = 100
options = [
('glowsticks', 10),
('jumbo glasses', 15),
('inflatable hammers', 25),
('dinosaur grabbers', 30),
('wooden snakes',35),
('foam swords', 40)
]
for name, cost in options:
while True:
count = int(input(f"Tell me again how many {name} you want? "))
if count * cost <= balance:
balance -= count * cost
print( "Your balance is now", balance, "points.")
break
else:
print(f"Too many {name}, please change your amount")
uj5u.com熱心網友回復:
假設 n 是您要購買的木蛇的數量
您正在檢查余額是否小于 n(所以如果我想要 2 條蛇,只要余額大于等于 2,它就會讓我購買木蛇)但是您需要檢查成本購買 n 條蛇小于余額。
這當然適用于您程式中的所有專案。
您還可以elif
用else
宣告替換您的宣告,就好像成本不低于或等于余額一樣,您肯定知道,無需檢查它是否會更高。
我將以木蛇為例
代替
#How many wooden snakes the player wants
prize1 = int(input("Tell me again how many wooden snakes you want "))
if prize1 <= balance:
#buy the item
elif prize1 > balance:
print("Too many wooden snakes, please change your amount")
break
和
#How many wooden snakes the player wants
prize1 = int(input("Tell me again how many wooden snakes you want "))
if prize1 * 35 <= balance:
#buy the items
else:
print("Too many wooden snakes, please change your amount")
uj5u.com熱心網友回復:
編輯
根據@Tim 的反饋,這不是遞回的一個很好的用法,我已經使用相同的基本思想更新了我的代碼。
問題在于,在您的輸入中,您將物品的數量與積分的余額進行比較,而不是將他們想要的數量與可用物品的成本進行比較。
為了盡量減少代碼行數,您還可以創建一個帶有遞回的函式,以在數字無效時繼續要求輸入。我建議如下:
balance = 100
prices = {'glowsticks':10, #etc}
def check_balance(item, qty):
global balance
while (cost := prices[item] * qty) > balance:
qty = int(input("Too many, try again: "))
else:
balance -= cost
print(f"New balance is {balance}.")
return
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/347972.html