python while两个条件_Python while循环有2个变量条件

我认为在这种情况下使用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))

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值