Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- 2.11 Challenge Think of a real-world scenario where you need to work
- with data and find the average,
- minimum or maximum of a list of values.
- This could be using data that you work with in school, or for a
- hobby or other interest.
- Write an algorithm in plain English for your chosen function.
- Implement the function in Python and demonstrate it using randomly-generated
- data or data from a real world problem.
- """
- # my program invites a user to enter data and then calculate min, max and mean
- """
- approach
- get input from user until stats or quit is entered
- capture entries in a list
- when stats entered calculate
- min, max and mean
- and print out
- when quit entered, just quit the progamme
- """
- def calc_min(datalist):
- currmin = datalist[0] # captures a start value that's in the list
- for item in datalist:
- if item < currmin:
- currmin = item
- return (currmin)
- def calc_max(datalist):
- currmax = datalist[0] # captures a start value that's in the list
- for item in datalist:
- if item > currmax:
- currmax = item
- return (currmax)
- def calc_mean(datalist):
- listlength=0
- listsum = 0
- for item in datalist:
- listsum += item
- listlength += 1
- listavg = listsum/listlength
- return (listsum)
- return (listlength)
- return (listavg)
- def calc_all (datalist):
- print(f"the data you entered was {datalist} it has {len(datalist)} items")
- print(f"the smallest (MINIMUM) value was {calc_min(datalist)}")
- print(f"the largest (MAXIMUM) value was {calc_max(datalist)}")
- print(f"and the average (MEAN) value was {calc_mean(datalist)}")
- print("Thank you for using me!!")
- datalist=[]
- baddata=[]
- #test data
- #for x in range (0, 30):
- # datalist.append(randint(0, 10)) # Generate a random number from 0 to 10 and append to scores
- #print(datalist)
- print(
- """
- This is my stats machine, keep entering numbers
- and I will then produce some stats:-
- enter STATS to produce the min, max and mean
- enter QUIT to leave the program
- """)
- data_entry = input("Please enter a number or STATS or QUIT > " )
- while True: #data_entry.upper() not in ("STATS","QUIT"): # testing entry
- try:
- if data_entry.upper() == "STATS": # testing for stats being entered
- calc_all(datalist)
- break
- elif data_entry.upper() == "QUIT": # testing for a simple quit
- break
- elif float(data_entry): # seeing if data _entry is a float
- datalist.append(float(data_entry))
- data_entry = input("Please enter a number or STATS or QUIT > " )
- elif int(data_entry): # seeing if data_entry is an integer
- datalist.append(float(data_entry))
- data_entry = input("Please enter a number or STATS or QUIT > " )
- except:
- print("invalid entry > " + str(data_entry)) # bad data entry trap and ouptup what has been entered
- baddata.append(data_entry) # copy the bad data to separate list
- data_entry = input("Please enter a number or STATS or QUIT > " )
- # testing separate functions
- #calc_min(datalist)
- #calc_max(datalist)
- #calc_mean(datalist)
- #calc_all(datalist)
- if len(baddata):
- print(f"The bad data you entered was {baddata}, {len(baddata)}items")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement