如何检查 Python 中的成本是否低于零
How to Check if Costs go Below Zero in Python
作为我 class 中的一项最终任务,我们的任务是编写一款可以在游乐园排队玩的游戏。完成游戏后,我们需要让玩家有机会购买一定数量的 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
这是我的代码中出现问题的部分。我想让玩家在获胜后输入他们想要的每个奖品的数量。如果他们负担不起他们想要的金额,因为这会导致您的余额低于零,或者超过您的余额,程序会要求您更改金额以适应它。
P.S。本例中余额为 100 的原因是出于演示目的。它可能并不总是等于该数字,并且可能更高或更低,具体取决于玩家在整个游戏中的表现。
编程中有一条关键规则叫做 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")
编辑
根据@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
假设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")
作为我 class 中的一项最终任务,我们的任务是编写一款可以在游乐园排队玩的游戏。完成游戏后,我们需要让玩家有机会购买一定数量的 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
这是我的代码中出现问题的部分。我想让玩家在获胜后输入他们想要的每个奖品的数量。如果他们负担不起他们想要的金额,因为这会导致您的余额低于零,或者超过您的余额,程序会要求您更改金额以适应它。
P.S。本例中余额为 100 的原因是出于演示目的。它可能并不总是等于该数字,并且可能更高或更低,具体取决于玩家在整个游戏中的表现。
编程中有一条关键规则叫做 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")
编辑
根据@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
假设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")