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

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

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

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

Q&A

解決済

1回答

1043閲覧

python実行時のエラーについて

sary

総合スコア12

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

0グッド

0クリップ

投稿2020/03/31 07:34

前提・実現したいこと

pythonで辞書とファイル入力を使ったプログラムを作っています。macで「python3 ii.py」と実行したところエラーが出ました。

発生している問題・エラーメッセージ

Traceback (most recent call last): File "ii.py", line 9, in <module> student_name, points_str = line.split(',') ValueError: too many values to unpack (expected 2)

該当のソースコード

python

1# -*- coding: utf-8 -*- 2open_file = open('point.txt') 3raw_data = open_file.read() 4open_file.close() 5point_data = raw_data.splitlines() 6 7point_dict = {} 8for line in point_data: 9 student_name, points_str = line.split(',') 10 point_dict[student_name] = points_str 11 12score_dict = {} 13for student_name in point_dict: 14 point_list = point_dict[student_name].split(',') 15 subject_number = len(point_list) 16 total = 0 17 for point in point_list: 18 total = total + int(point) 19 average = total / subject_number 20 score_dict[student_name] = (total, average, subject_number) 21 22evaluation_dict = {} 23for student_name in score_dict: 24 score_data = score_dict[student_name] 25 total = score_data 26 average = score_data[1] 27 subject_number = score_data[2] 28 29 excellent = subject_number * 80 30 good = subject_number * 0.65 31 if total >= excellent: 32 evaluation = 'excellent' 33 if total >= good: 34 evaluation = 'good' 35 else: 36 evaluation = 'bad' 37 evaluation_dict[student_name] = evaluation 38 39file_name = 'evaluation.txt' 40output_file = open(file_name, 'w') 41for student_name in score_dict: 42 score_data = score_dict[student_name] 43 total = score_data[0] 44 45 evaluation = evaluation_dict[student_name] 46 47 text = '[{}] total: {}, evaluation: {}\n'.format(student_name, total, evaluation) 48 output_file.write(text) 49 50output_file.close() 51print('評価結果を{}に出力しました'.format(file_name))

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

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

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

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

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

yureighost

2020/03/31 08:01

エラーの直接原因としてはsplitが一つの戻り値しか返さないのに student_name, points_strの二つの変数で受け取ろうとしているためです。
sary

2020/03/31 08:11

ありがとうございました。おかげで直りました
guest

回答1

0

自己解決

、を:になおしました

投稿2020/04/01 12:32

sary

総合スコア12

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問