Advertisement
namchef

py4inf_ch5

Sep 26th, 2011
279
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.19 KB | None | 0 0
  1. # ex. 5_1
  2. # 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.
  4. # 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.
  5.  
  6.  
  7. x=raw_input('Please input number\n')
  8. while x!='done':
  9.     if x.isdigit()==False:
  10.         x=raw_input('Please give me a real number\n')
  11.     else: x=raw_input('Please input another number \n')
  12. print 'You\'re done'
  13.  
  14.  
  15. # ex. 5_2
  16. # 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.
  17.  
  18.  
  19.  
  20. values = [1, 3, 5, 7, 9, 11]
  21.  
  22. def min_val(values):
  23.     smallest=None
  24.     for value in values:
  25.         if smallest is None or value<smallest:
  26.             smallest=value
  27.     return smallest
  28.  
  29.  
  30. def max_val(values):
  31.     biggest=None
  32.     for value in values:
  33.         if biggest is None or values>biggest:
  34.             biggest=value
  35.     return biggest
  36.  
  37. def return_m_M (values):
  38.     x=min_val(values)
  39.     y=max_val(values)
  40.     print x, y
  41.  
  42.  
  43. return_m_M(values)
  44.  
  45.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement