概述
- 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.
Enter a number: 4
Enter a number: 5
Enter a number: bad data Invalid
input
Enter a number: 7
Enter a number: done
16 3 5.333333333333333
total = 0
count = 0
while True:
num=input('Enter a number:')
if num=="done":
break
try:
fn=int(num)
except:
print("Invalid input")
continue
count +=1
total +=fn
average=total/count
print(total,count,average)
- Exercise 2: Write another program that prompts for a list of numbers as above and at the end prints out both the maximum and minimum of the numbers instead of the average.
largest = None
smallest = None
while True:
num=input('Enter a number:')
if num=="done":
break
try:
fn=int(num)
except:
print("Invalid input")
continue
if largest is None:
largest=fn
if fn>largest:
largest=fn
if smallest is None:
smallest=fn
if fn<smallest:
smallest=fn
print(largest,smallest)
觉得自己很不足啊,有些东西尽管有思路,但是知识不够要到处查资料才能做出来,而且都是很基础的知识,还是要打好基础!
最后
以上就是结实篮球为你收集整理的PY4E exercise chapter5 Iterations的全部内容,希望文章能够帮你解决PY4E exercise chapter5 Iterations所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复