bank = 0 number = 0
而True:
try:
number = int(raw_input("Enter an integer ( such as 49 or 3 or 16) \n"))
bank = bank + number
print 'You entered--- ', number, 'Your running total is ', bank
except:
if number == 'done':
print 'Done'
else:
if number == 'done':
print 'Done'
else:
print 'Your entry was non-numberic. Please enter a number.'
当我运行它并输入“done”时,我得到“else:”响应和一个新的输入行。如果数字==“完成”
,我不会从获得“完成”打印答案 0 :(得分:0)
用python 3编写的答案
使用的异常是ValueError,因为编译器在第7行中完成转换后捕获了此错误,所以我只是在第19行添加了 continue 使其跳过错误并返回开始。
bank = 0
count = 0
while True:
try:
number = input('enter an integer:\n')
if number != 'done':
bank += int(number)
print('you entered -- ', number, 'your total is ', bank)
count += 1
elif number == 'done':
print('Done')
print('you entered %d numbers' % count)
print('Your total is %s' % bank)
average = bank/count
print('Your average is %.02f' % average)
break
except ValueError:
print('oops!! that was not an integer')
continue