scipiguy

Programming 102: Max\Min\Mean functions

Jun 27th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.56 KB | None | 0 0
  1. #Weather data from Cambridge UK (Source: https://www.ncdc.noaa.gov/)
  2.  
  3. # set up functions to take the relevant list
  4. def minimum(the_list):
  5.     value = sorted(the_list)[0]
  6.     return value
  7.  
  8. def maximum(the_list):
  9.     value = sorted(the_list, reverse=True)[0]
  10.     return value
  11.    
  12. def mean(the_list):
  13.     total = 0
  14.     for x in range(len(the_list)):
  15.         total += the_list[x]
  16.     value = round(total/len(the_list), 2)
  17.     return value
  18.  
  19. # list the 3 data sets
  20. max_temps = [8,9,12,15,18,21,23,23,20,16,11,8]
  21. min_temps = [2,2,3,5,8,11,13,13,10,8,4,2]
  22. rain_days = [10,8,7,7,8,8,8,9,6,8,10,8]
  23.  
  24. # menu to choose which data set to work on
  25. print("Which data would you like to work with? Select option 1, 2 or 3 below:\n")
  26. the_choice = input("1: Maximum monthly temperatures\n2: Minimum monthly temperatures\n3: Number of rainy days\n\n> ")
  27.  
  28. if the_choice == "1":
  29.     the_list = max_temps
  30. elif the_choice == "2":
  31.     the_list = min_temps
  32. elif the_choice == "3":
  33.     the_list = rain_days
  34. else:
  35.     print("Invalid selection")
  36.    
  37. # Establish which procedure is needed
  38. print("For this data set, would you like to know the maximum value, minimum value or average value?")
  39. the_choice = input("1: Maximum value\n2: Minimum value\n3: Average value\n\n> ")
  40.  
  41. if the_choice == "1":
  42.     print("\nThe maximum value is: " + str(maximum(the_list)))
  43. elif the_choice == "2":
  44.     print("\nThe minimum value is: " + str(minimum(the_list)))
  45. elif the_choice == "3":
  46.     print("\nThe mean value is: " + str(mean(the_list)))
  47. else:
  48.     print("\nInvalid selection")
  49. print()
Advertisement
Add Comment
Please, Sign In to add comment