Python 连续输入

Python continuous input

我的 while 循环有问题。基本上,程序应该不断要求用户输入商品价格,直到他们输入 'Done',然后打印总帐单。对于上下文,我现在将我的代码。

a = float(input('Price? '))
count = 0
while a > 0:
  b = float(input('Price? '))
  count += b
if a == 'Done':
   print('Total is $', count)
count = 0
while True:
    a = input('Price? ')
    if a == 'Done':
        print('Total is $', count)
        break
    count += float(a)

请注意,如果用户输入的字符串既不是“Done”也不是浮点文字,则此代码会中断。为此,您需要用 try / except 块包围 count += float(a) 行。