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.
- '''
- sum1=0
- total=0
- average=0
- a=raw_input('Enter a number:')
- while a!='done':
- try:
- a=float(a)
- sum1=sum1+a
- total=total+1
- except:
- print "Please insert a number"
- a=raw_input("Enter a number:")
- print "Sum:",sum1
- print "Total:",total
- print "Average:",float(sum/total)
- '''
- 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.
- '''
- a=raw_input('Enter a number:')
- min=a
- max=a
- while a!='done':
- try:
- a=float(a)
- if(float(a)>float(max)):
- max=a
- if(float(a)<float(min)):
- min=a
- except:
- print "Please insert a number"
- a=raw_input("Enter a number:")
- print "Max:",max
- print "Min:",min
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement