Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Exercise 5.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.
- lt=[]
- total=0
- count=0
- avg=float()
- while True:
- i=input('Enter a number:')
- try:
- if i=='done':break
- if int(i)==str():continue
- lt.append(i)
- except:
- print("bad data\ninvalid input")
- for i in lt:
- total+=int(i)
- count+=1
- avg=total/count
- print(total,count,format(avg,'.7g'))
- ------------------------------------------------------------------------------------------------
- #Exercise 5.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.
- ls=[]
- while True:
- i=input("Enter a number:")
- try:
- if i=='done':break
- if int(i)==str():continue
- ls.append(i)
- except:
- print("Bad data\nInvalid Data")
- print(ls)
- k=0
- for i in ls:
- if int(i)>=k:
- k=int(i)
- l=k
- for i in ls:
- if int(i)<=l:
- l=int(i)
- print("Minimum:",l,"Maximum:",k)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement