Advertisement
Guest User

Untitled

a guest
Jul 10th, 2012
1,126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.14 KB | None | 0 0
  1. '''
  2. Exercise 5.1 Write a program which repeatedly reads numbers until the user enters “done”.
  3. 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.
  4. '''
  5. sum1=0
  6. total=0
  7. average=0
  8. a=raw_input('Enter a number:')
  9. while a!='done':
  10.     try:
  11.         a=float(a)
  12.         sum1=sum1+a
  13.         total=total+1
  14.     except:
  15.         print "Please insert a number"
  16.     a=raw_input("Enter a number:")
  17. print "Sum:",sum1
  18. print "Total:",total
  19. print "Average:",float(sum/total)
  20.  
  21. '''
  22. Exercise 5.2 Write another program that prompts for a list of numbers as above and at the end
  23. prints out both the maximum and minimum of the numbers instead of the average.
  24. '''
  25. a=raw_input('Enter a number:')
  26. min=a
  27. max=a
  28. while a!='done':
  29.     try:
  30.         a=float(a)
  31.         if(float(a)>float(max)):
  32.             max=a
  33.         if(float(a)<float(min)):
  34.             min=a
  35.     except:
  36.         print "Please insert a number"
  37.     a=raw_input("Enter a number:")
  38. print "Max:",max
  39. print "Min:",min
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement