这是我刚刚编写的一个小程序的代码,用于测试我学到的一些新东西。
while 1:
try:
a = input("How old are you? ")
except:
print "Your answer must be a number!"
continue
years_100 = 100 - a
years_100 = str(years_100)
a = str(a)
print "You said you were "+a+", so that means you still have "+years_100+" years"
print "to go until you are 100!"
break
while 2:
try:
b = str(raw_input('Do you want to do it again? If yes enter "yes", otherwise type "no" to stop the script.'))
except:
print 'Please try again. Enter "yes" to do it again, or "no" to stop.'
continue
if b == "yes":
print 'You entered "yes". Script will now restart... '
elif b == "no":
print 'You entered "no". Script will now stop.'
break
它适用于 for 循环。如果您输入的不是数字,它会告诉您只允许输入数字。
但是,在第二个循环中,它会要求您输入是或否,但如果您输入不同的内容,它只会重新启动循环而不是在之后打印消息
except:
我做错了什么以及如何解决它以显示我告诉它的消息?