我认为在这种情况下使用while True不是一个好的做法。在
我建议您使用min(moneyihave, coffee)预先检查每个最小值,然后使用range方法创建一个循环:min_val = min(moneyihave, coffee)
for i in range(0, min_val):
print(i) # Goes from 0 to min_val
我建议您另一种方法是直接在while语句中设置循环条件:
^{pr2}$
但是,看看你的代码,我发现了一些其他问题:您有一个coffeeprice,但从未使用过它。我想你的目的是每次“顾客”买咖啡时从moneyihave中扣除。在
用户在每次循环迭代时都会输入资金,但是这个输入的值只用于检查第一个if...else块。我认为每次都设置money值,但从moneyihave中扣除一些金额,这有点令人困惑。在
你有:print('you have {} left in your pocket'.format(moneyihave-200))在完成moneyihave = moneyihave - 200之后,这样你就向用户显示了一个错误的金额。在
短语print('our coffee is 300 dollars')与提供的咖啡价格不符。同样,我认为这里的问题不是使用这个变量
另外,在您的第一个if...else块中,if和{}上,您都要花一杯咖啡和一些钱,因此我建议您对如下内容进行重构:# Spend money if possible
if money >= 200:
coffee = coffee - 1
moneyihave = moneyihave - 200 # I think here you should use coffeeprice
else:
print('our coffee is 300 dollars') # I think here you should use coffeeprice
print('we have {} coffees left'.format(coffee))
print('you have {} left in your pocket'.format(moneyihave)) # Since he can't have negative money, this should always be 0
# User still have money left
if moneyihave > 0:
print('please take your change {} and here is your coffee'.format(money-200))
print('you have {} left in your pocket'.format(moneyihave-200))
else:
print('here is your coffee, we have {} coffees left'.format(coffee))
print('you have {} left in your pocket'.format(moneyihave-200))