#問題点・解決したい事
コード1で実装したデータをテキストファイルに出力するためにwith open("sample.txt", "a") as f:を実装しました。(コード2)しかし'_io.TextIOWrapper'というエラーが出てしまいます。
データを書き出すためにはwith open - とf.write(result)- の間にwhile文を書くという事を学習したのですが、なぜエラーが起きてしまったのかわかりません。
お手数ですが教えて頂けないでしょうか。よろしくお願いいたします。
#実装した計算(コード1)
python
1limit = 999 2x = 0 3 4def f(x): 5 return x**2 6 7while f(x) < limit: 8 x += 1 9 print("{:.1f} {:.1f}\n".format(x, f(x)))
#エラーが生じたコード(コード2)
python
1limit = 999 2x = 0 3 4def f(x): 5 return x**2 6 7with open("sample.txt", "a") as f: 8 while f(x) < limit: 9 x += 1 10 result = "{:.1f} {:.1f}\n".format(x, f(x)) 11 f.write(result)
#コード2のエラー内容
Python
1--------------------------------------------------------------------------- 2TypeError Traceback (most recent call last) 3<ipython-input-3-760ec0e3f893> in <module>() 4 6 5 7 with open("sample.txt", "a") as f: 6----> 8 while f(x) < limit: 7 9 x += 1 8 10 result = "{:.1f} {:.1f}\n".format(x, f(x)) 9 10TypeError: '_io.TextIOWrapper' object is not callable 11
#使用環境
Jupyter Notebook
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。