python:getting the average of n

Asked 1 years ago, Updated 1 years ago, 324 views

I'm working on python code and I'm writing code that finds the highest, lowest, and average value of the name, score, and score from the data I get.
I can print the name, score, highest and lowest values, but I can't write the code that gives the average value.I've looked at some websites, but none of them work.I tried the code below using the module, but it doesn't work.Do you have any suggestions?

 from statistics import median
from path import isan
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)

python

2022-12-17 18:26

1 Answers

You can get the average by using in .

Rewrite import median to import mean and mid(scores['score') to mean(scores['score').

sample code

 from statistics import mean

if__name__=='__main__':
    score=[10,20,30]
    print(score)
    print("max score is:", max(score))
    print("min score is:",min(score))
    print("average score is", mean(score))

Run Results

 [10,20,30]
max score is:30
min score is —10
average score is 20

References


2022-12-18 06:33

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.