0

入力から得た数字の平均値、最大値、最小値を求めるコードを書きたいのですが、上手くいきません。
どのように修正をすればいいですか?
現段階では最大値を求めるだけのコードをテストとしか書いていないです。おそらく最大値を出すコードを書く場所は適切な場所ではないと思います。どこに書けばいいのでしょうか?

def listofname():
    members, scores = {'name': []}, {'Score': []}
    while 1:
        Students = input("Enter a name or enter 'done' when finished ")
        if Students == 'done': break
        members['name'] += [Students]
        Score = input('Enter score ')
        scores['Score'] += [int(Score)]

if __name__ == '__main__':
    listofname()

    maxOfValue = scores[0]

    for i in range(1,len()):

        if(maxOfValue < scores[i]):

            maxOfValue = scores[i]

    print("max number:", scores)

1 件の回答 1

3

最大値と最小値は max(), min() 関数、平均値は statistics 数理統計関数 モジュールの mean() が利用可能です。

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()
    maxScore = max(scores['score'])
    print("max number:", maxScore)
2
  • このコードだとmax numberしか表示されないのですが。自分なりに少し手を加えて、編集してみたのですがうまくいきませんでした。ScoreListの内容もプリントしたいのですが、どこにScoreList()を記入すればいいのでしょうか?
    – yojozaok21
    2022年12月17日 19:27
  • 1
    scores = ScoreList() 以降に print(scores) を入れてください。また、「最大値と最小値は max(), min() 関数、平均値は statistics 数理統計関数 モジュールの mean() が利用可能です。」をよく読んで下さい。
    – metropolis
    2022年12月17日 19:30

この質問に回答するには、ログインする必要があります。

求めていた回答ではありませんか? のタグが付いた他の質問を参照する。