无限循环和添加 raw_input 结果的问题

Problems with infinite-loop and adding raw_input results

现在,我正在尝试一些循环过程中的练习。不幸的是,我遇到了一些问题,希望大家能帮助我。

问题:

编写具有以下输出的脚本:

Welcome to the receipt program!
Enter the value for the seat ['q' to quit]: five
I'm sorry, but 'five' isn't valid. Please try again.
Enter the value for the seat ['q' to quit]: 12
Enter the value for the seat ['q' to quit]: 15
Enter the value for the seat ['q' to quit]: 20
Enter the value for the seat ['q' to quit]: 30
Enter the value for the seat ['q' to quit]: 20
Enter the value for the seat ['q' to quit]: q

Total: 

这是我的代码:

print "Welcome to the receipt program"
while True:
    value = raw_input('Enter the value for the seat [Press q to quit]: ')

    if value == 'q':
        break

    print 'total is {}'.format(value)
    while not value.isdigit():
        print "I'm sorry, but {} isn't valid.".format(value)
        value = raw_input("Enter the value for the seat ['q' to quit]: ")

我面临的问题:

  1. 当我运行代码时,我按了q。什么都没有显示。
  2. 如果值 == 'q',如何添加值的总金额?

您正在循环中打印总计,而您最终想这样做。另外,您想累积您的总数:

print "Welcome to the receipt program"

total = 0
while True:
    value = raw_input('Enter the value for the seat [Press q to quit]: ')

    if value == 'q':
        break

    if value.isdigit():
        total += int(value)
    else:
        print "I'm sorry, but {} isn't valid.".format(value)

print 'total is {}'.format(total)