Python while循环,有2个变量条件

时间:2018-05-17 22:09:54

标签: python while-loop

我有以下为练习创建的代码,我的目的是尝试使用while循环并确保moneyihavecoffee的数量都被扣除,直到一个达到0,这将打破循环。

我在这里的问题是moneyihave没有被正确扣除。当我为coffee输入200时,它首先扣除双倍金额。但是当我输入大于200时,似乎正确地从moneyihave中扣除了金额,但在moneyihave变为-200之前它不会中断循环。

我是python的新手,如果有人能指引我,我会非常感激!

coffee = 20
coffeeprice = 200
moneyihave = 40000

while True:
    money = int(input('insert your money: '))
    if money == 200:
        coffee = coffee - 1
        moneyihave = moneyihave - 200

        print('here is your coffee, we have {} coffees left'.format(coffee))
        print('you have {} left in your pocket'.format(moneyihave-200))

    elif money>200:

        print('please take your change {} and here is your coffee'.format(money-200))        
        print('you have {} left in your pocket'.format(moneyihave-200))   

        coffee = coffee -1
        moneyihave = moneyihave - 200

    else:

        print('our coffee is 300 dollars')
        print('we have {} coffees left'.format(coffee))
        print('you have {} left in your pocket'.format(moneyihave))


    if not coffee:      

        print('please come back, we do not have coffee anymore')
        break

    elif moneyihave<=0:
        print('no more money')
        break

2 个答案:

答案 0 :(得分:1)

恕我直言,我认为在这种情况下使用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语句中设置循环条件:

# Since you sell coffee's without checking if you still have some, the condition should be coffee > 0.
while moneyihave >= 0 and coffee > 0:
    # Do stuff...
    # Deduct variables:
    moneyihave -= 1
    coffee -= 1

但是,看看你的代码,我可以看到其他一些问题:

  • 您有一个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区块中,ifelif都会花一杯咖啡和一些钱,因此我建议您重新构建以下内容:

# 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))

答案 1 :(得分:0)

我编辑了你的代码,这似乎有效。问题是,当您格式化代码时,您使用了.format(moneyihave-200)。因此,当您打印结果时,您最初减去400,尽管您只减去200.您的打印值基本上与实际值不匹配。下面的代码将简化这一过程并为您提供所需的输出:

coffee = 20
coffeeprice = 200
moneyihave = 40000

while True:
    money = int(input('insert your money: '))
    if money == 200:
        coffee -= 1
        moneyihave -= 200

        print('here is your coffee, we have {} coffees left'.format(coffee))
        print('you have {} left in your pocket'.format(moneyihave)) 


    else:

        print('our coffee is 200 dollars')
        print('we have {} coffees left'.format(coffee))
        print('you have {} left in your pocket'.format(moneyihave))


   if coffee <= 0:      

      print('please come back, we do not have coffee anymore')
      break

   elif moneyihave <=0:
      print('no more money')
      break

每次迭代循环时,这是你的输出:

insert your money: 200
here is your coffee, we have 19 coffees left
you have 39800 left in your pocket
insert your money: 200
here is your coffee, we have 18 coffees left
you have 39600 left in your pocket
insert your money: 200
here is your coffee, we have 17 coffees left
you have 39400 left in your pocket
insert your money: 200

最后我们打破了循环:

you have 0 left in your pocket
no more money
>>>