Advertisement
timber101

StatsProg

Dec 26th, 2020
901
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.30 KB | None
  1. """
  2. 2.11 Challenge Think of a real-world scenario where you need to work
  3. with data and find the average,
  4. minimum or maximum of a list of values.
  5. This could be using data that you work with in school, or for a
  6. hobby or other interest.
  7. Write an algorithm in plain English for your chosen function.
  8. Implement the function in Python and demonstrate it using randomly-generated
  9. data or data from a real world problem.
  10. """
  11.  
  12. # my program invites a user to enter data and then calculate min, max and mean
  13.  
  14. """
  15. approach
  16. get input from user until stats or quit is entered
  17.  
  18. capture entries in a list
  19. when stats entered calculate
  20. min, max and mean
  21. and print out
  22.  
  23. when quit entered, just quit the progamme
  24. """
  25.  
  26. def calc_min(datalist):
  27.     currmin = datalist[0]  # captures a start value that's in the list
  28.     for item in datalist:
  29.         if item < currmin:
  30.             currmin = item
  31.     return (currmin)
  32.    
  33. def calc_max(datalist):
  34.     currmax = datalist[0]  # captures a start value that's in the list
  35.     for item in datalist:
  36.         if item > currmax:
  37.             currmax = item
  38.     return (currmax)
  39.  
  40. def calc_mean(datalist):
  41.     listlength=0
  42.     listsum = 0
  43.     for item in datalist:
  44.         listsum += item
  45.         listlength += 1
  46.     listavg = listsum/listlength
  47.     return (listsum)
  48.     return (listlength)
  49.     return (listavg)
  50.    
  51. def calc_all (datalist):
  52.     print(f"the data you entered was {datalist} it has {len(datalist)} items")
  53.     print(f"the smallest (MINIMUM) value was {calc_min(datalist)}")
  54.     print(f"the largest (MAXIMUM) value was {calc_max(datalist)}")
  55.     print(f"and the average (MEAN) value was {calc_mean(datalist)}")
  56.     print("Thank you for using me!!")
  57.  
  58.  
  59. datalist=[]
  60. baddata=[]
  61.  
  62. #test data
  63. #for x in range (0, 30):
  64. #  datalist.append(randint(0, 10))    # Generate a random number from 0 to 10 and append to scores
  65.  
  66. #print(datalist)
  67.  
  68. print(
  69. """
  70. This is my stats machine, keep entering numbers
  71. and I will then produce some stats:-
  72. enter STATS to produce the min, max and mean
  73. enter QUIT to leave the program
  74.  
  75. """)
  76.  
  77. data_entry = input("Please enter a number or STATS or QUIT > " )
  78.  
  79. while True: #data_entry.upper() not in ("STATS","QUIT"): # testing entry
  80.     try:
  81.         if data_entry.upper() == "STATS":  # testing for stats being entered
  82.             calc_all(datalist)
  83.             break
  84.         elif data_entry.upper() == "QUIT":  # testing for a simple quit
  85.             break
  86.         elif float(data_entry):  # seeing if data _entry is a float
  87.             datalist.append(float(data_entry))
  88.             data_entry = input("Please enter a number or STATS or QUIT > " )
  89.         elif int(data_entry):  #  seeing if data_entry is an integer
  90.             datalist.append(float(data_entry))
  91.             data_entry = input("Please enter a number or STATS or QUIT > " )
  92.     except:
  93.         print("invalid entry > " + str(data_entry))  # bad data entry trap and ouptup what has been entered
  94.         baddata.append(data_entry)  # copy the bad data to separate list
  95.         data_entry = input("Please enter a number or STATS or QUIT > " )
  96.            
  97. #  testing separate functions
  98. #calc_min(datalist)
  99. #calc_max(datalist)
  100. #calc_mean(datalist)
  101.  
  102. #calc_all(datalist)
  103.  
  104. if len(baddata):
  105.     print(f"The bad data you entered was {baddata},  {len(baddata)}items")
  106.  
Advertisement
RAW Paste Data Copied
Advertisement