I have been working on a code that collecting students name and their score. Then printing their name, their score, their highest score, lowest score, and average score. I have completed to write a code that printing name,score, highest and lowest score.I have not completed average one. I have tried some codes but all of them have not worked so far. could you give me some advice or some code that I can use for the code, please? I visited some website to find some code that I can use for my code. In the code below I have tried with importing module, but it does not work. I look forward to hearing some great advice from you guys.
from statistics import median
from math import isnan
from itertools import filterfalse
def ScoreList():
scores = {'name': [], 'score': []}
while True:
name = input("Enter a name or enter 'done' when finished ")
if name == 'done': break
scores['name'] = [name]
score = input('Enter score ')
scores['score'] = [int(score)]
return scores
if __name__ == '__main__':
scores = ScoreList()
print(scores)
maxScore = max(scores['score'])
print("max score is:", maxScore)
minScore = min(scores['score'])
print("min score is:", minScore)
midScore = mid(scores['score'])
print("average score is", midScore)
I have visited some website to find some codes examplpe that I can use for my code and all of them did not work so far.
CodePudding user response:
Currently your dictionary only stores a list of names and a list of scores. This is probably not the best way because there will be multiple scores associated with one name. You could instead use the name
as the key in the dictionary, and a list of scores as the value. Define scores = {}
in your main function. In the input loop you have to ask, if a name is already present in the dict, if so, append the score, otherwise make a new name entry
name = input("Enter a name or enter 'done' when finished ")
score = int(input('Enter score '))
if name in scores:
scores[name].append(score)
else:
scores[name] = [score]
Now you can ask for min, max and average score of a student by
for student_name in scores:
student_min = min(scores[student_name])
student_max = max(scores[student_name])
student_avg = sum(scores[student_name]) / len(scores[student_name])
print(student_name, student_min, student_max, student_avg)
CodePudding user response:
If your intent is to allow the user to enter one or more scores for one or more students and then calculate the max, min, and average across all students then something like this will work. score_dict
uses student name for its keys and one or more scores per student for its values. chain
is an easy way to extract those values into a list which makes calculating the results straightforward.
from collections import defaultdict
from itertools import chain
from statistics import mean
def get_score_dict():
scores = defaultdict(list)
while (name := input("Enter a name or enter 'done' when finished: ")) != "done":
score = input("Enter score: ")
scores[name].append(int(score))
return scores
if __name__ == '__main__':
score_dict = get_score_dict()
score_list = list(chain.from_iterable(score_dict.values()))
print(score_list)
print(f"Max score: {max(score_list)}")
print(f"Min score: {min(score_list)}")
print(f"Avg score: {mean(score_list)}")