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

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

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

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

Q&A

解決済

2回答

347閲覧

Python3 辞書型にデータを追加するプログラム 実行できない理由はなぜでしょうか?

gaijin

総合スコア30

Python 3.x

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

0グッド

0クリップ

投稿2018/10/30 14:41

python3を学習している初心者です。
参考書の通りに打ち込んだのですが、実行すると

Traceback (most recent call last):
File "05_file.py", line 2, in <module>
raw_data = open_file.read()
AttributeError: 'str' object has no attribute 'read'

このようにエラーメッセージが出ます。
どうぞご教授をお願いしたいです。

下記のように定義いたしました。

open_file = ('point.txt')
raw_data = open_file.read()
open_file.close()
point_data = raw_data.splitlines()
point_dict = {}
for line in point_data:
student_name, points_str = line.split(':')
point_dict[student_name] = points_str

score_dict = {} for student_name in point_dict: point_list = point_dict[student_name].split(',') subject_number = len(point_list) total = 0 for point in point_list: total = total + int(point) average = total / subject_number score_dict[student_name] = (total, average, subject_number)

evaluation_dict = {}
for student_name in score_dict:
score_data = score_dict[student_name]
total = score_data[0]
average = score_data[1]
subject_number = score_data[2]

excellent = subject_number * 100 * 0.8 good = subject_number * 100 * 0.65 if total >= excellent: evaluation = 'Excellent' elif total >= good: evaluation = 'Good' else: evaluation = 'Bad' evaluation_dict[student_name] = evaluation

file_name = 'evaluation.txt'
output_file = open(file_name, '')

for student_name in score_dict:
score_data = score_dict[student_name]
total = score_data[0]

evaluation = evaluation_dict[student_name] text = '[{}] total: {}, evaluation: {}\n'.format(student_name, total, evaluation) output_file.write(text) output_file.close() print('評価結果を{}に出力しました'.format(file_name))

調べたところopen_fileを定義している場所でなにか間違いがあるとのことですが、まだ理解できていません。

Takanori Suzuki:100,88,81
Takayuki Shimizukawa:77,94,85
Mitsuki Sugiya:80,52,99

このように教本通り打ち込み
point.txtというものを同じ階層に保存しております。

どうぞよろしくお願い致します。

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

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

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

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

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

guest

回答2

0

python

1open_file = ('point.txt')

python

1open_file = open('point.txt')

とする必要があるのでは。

投稿2018/10/30 14:44

hayataka2049

総合スコア30933

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

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

gaijin

2018/10/30 14:53

こんなに早く連絡をいただけ本当にありがとうございます! そして本当に単純な入力ミスで情けないです。 すぐに試してみました。 Traceback (most recent call last): File "05_file.py", line 43, in <module> evaluation = evaluation_dict[student_name] KeyError: 'Takanori Suzuki' このように続いてエラーが出たのですが、何かこちらも間違っているのでしょうか? 本当に幼稚な質問ばかり聞いてしまっているようでしたら申し訳ありません。
hayataka2049

2018/10/30 14:56 編集

現状コードが読めないので、まずはLouiS0616さんの回答を参考に質問文を修正してください。新しいエラーが出たならその件についても追記すると良いでしょう
gaijin

2018/10/30 15:00

はい、そのようにまた追記いたします。 本当にありがとうございます! 感謝いたします!
guest

0

ベストアンサー

教本通り打ち込み

open_file = ('point.txt')open_file = open('point.txt')では?

コードの書き方について

teratailには、コードを見やすく表示する機能があります。
質問編集画面を開き、コードを選択した状態で<code>ボタンを押してください。
Python
特にPythonの場合、インデントが崩れるとコードの意味が変わってしまいます。

投稿2018/10/30 14:43

編集2018/10/30 14:44
LouiS0616

総合スコア35660

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

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

gaijin

2018/10/30 15:05

ありがとうございます! こちらで一度試してみます! 本当に色々と教えていただき感謝いております!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問