[英]Problems with infinite-loop and adding raw_input results
Right now, I am trying some exercises in looping process. 现在,我正在循环过程中尝试一些练习。 Unfortunately, I bumped into some problems and wish all of you can help me.
不幸的是,我遇到了一些问题,希望大家能帮助我。
The question: 问题:
Write a script which has these output: 编写一个具有以下输出的脚本:
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: $97
Here is my code: 这是我的代码:
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]: ")
The problem I am facing: 我面临的问题:
You are printing your total within the loop, while you want to do it in the end. 您要在循环中打印总计,而最后要打印。 Also, you want to accumulate your total:
另外,您要累计总计:
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.