#PY4E Chapter5 Exercises
练习题目如下
Exercise 1: Write a program which repeatedly reads numbers until the user enters “done” . Once “done” is entered, print out the total, count, and average of the numbers. If the user enters anything other than a number, detect their mistake using try and except and print an error message and skip to the next number.
求思无果,思路卡在“如何在不是数字的时候停止”,下面的代码是从这篇文章里copy的。
https://blog.csdn.net/yfh741853823
total = 0
count = 0
average = 0
while True:
inp = input('Enter a number: ')
if inp == 'done':
break
try:
inp = float(inp)
except:
print('%s is not a number' %inp)
continue
total = total + inp
count = count + 1
average = total/count
print('Total: %s Count: %s Average: %s' %(total,count,average))
总结: