# CS150 list example
# A set of functions for reading in scores and calculating
# various statistics from the input scores.
def get_scores():
"""
Read user input of numerical scores as floats into a list
Returns:
list of scores
"""
print("Enter the scores one at a time. Blank score finishes.")
scores = []
line = input("Enter score: ")
while line != '':
scores.append(float(line))
line = input("Enter score: ")
return scores
def manual_average(scores):
"""
Calculate the average of the values in list scores
Args:
scores: List of numeric scores
Returns:
Average score as a float
"""
# Compute "manually" without using built-in functions
sum = 0
count = 0
for score in scores:
sum = sum + score
count += 1
return sum / count
def assign_grades(scores):
"""
Assign letter grade calculate a letter grade for each numeric score in list.
If the score is greater than or equal to the average then it receives
and A. If it is less, an F.
Args:
scores: List of numeric scores
Returns:
List of letter grades assigned for each numeric score in corresponding
order
"""
# Construct additional list of same length with grades. List items at the same position correspond
# to the same person.
avg = manual_average(scores)
grades = []
for score in scores:
if score >= avg:
grades.append("A")
else:
grades.append("F")
return grades
def print_class_stats(scores):
"""
Print out various stats about the list of scores
Args:
scores: List of numeric scores
"""
print("Class max:", max(scores))
print("Class min:", min(scores))
print("Class average:", manual_average(scores))
print("Class grades:")
grades = assign_grades(scores)
# Since we are iterating through two lists in parallel, we use indices so we can
# access corresponding elements in each list
for i in range(len(grades)):
print(scores[i], " -> ", grades[i])
# Get the scores from the user and then print out the stats
scores = get_scores()
print("-" * 10)
print_class_stats(scores)