質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

Q&A

1回答

264閲覧

モジュールを用いてのプログラム作成

k.lolo

総合スコア4

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

0グッド

0クリップ

投稿2022/05/27 05:12

編集2022/05/27 05:31

下の入力値とモジュールを用いて合計点と平均点を出力するプログラムを作成しています。
エラーになってしまうのですがどこが間違っていますでしょうか?
#追記
intとリストを比較しているというエラーがでていますがよくわかりません。よろしくお願いいたします。

入力値

88
95
55
35
90
100
10
出力結果(Thresholdが60の場合)

合計点:373
平均点:93.25

コード import sys import csv global threshold threshold = 60 def get_scores(): reader = csv.reader(sys.stdin) line = list() for s in reader: line.append(s) return line def calc_total(scores): score2 = list() for n in scores: if n >= threshold: score2.append(n) elif n < threshold: continue total = sum(score2) return total def calc_count(scores): score2 = list() for n in scores: if n >= threshold: score2.append(n) elif n < threshold: continue lens = len(score2) return lens score_list = get_scores() total = calc_total(score_list) lens = calc_count(score_list) avg = total / lens print('合計点:{}'.format(total)) print('平均点:{}'.format(avg))

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

ozwk

2022/05/27 05:15 編集

エラーメッセージとそれを読んだ感想を質問に追記してください
guest

回答1

0

エラーメッセージを読むと、

python

1 if n >= threshold:

この部分で

TypeError: '>=' not supported between instances of 'list' and 'int'

listintを比較しているということを言っているのはわかると思います。

あなたとしてはおそらくintintを比較しているつもりでしょうが、
通常エラーメッセージは間違ったことを言わないので、nthresholdのどちらかがlistになっているはずです。
thresholdは冒頭で60と値を代入しているのでどちらかといえば怪しいのはnです。
というわけで、

python

1def calc_total(scores): 2 score2 = list() 3 for n in scores: 4 print(n) 5 if n >= threshold: 6 score2.append(n) 7 elif n < threshold: 8 continue 9 total = sum(score2) 10 return total

nprintしてみると、リストになっていることがわかると思います。

投稿2022/05/27 05:58

ozwk

総合スコア13528

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

k.lolo

2022/05/27 07:18

for line in reader   line=list(map(int, line)) return line に書き換えたのですが合計点88点平均点88点としか 出力されません。なぜでしょうか?
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問