中級 › AttributeError: 'NoneType' object has no attribute の原因と直し方 › パターン3
AttributeError: 'NoneType' object has no attribute の原因と直し方
Noneに対してメソッドを呼び出したときに出るエラーです。
1 user = {"name": "Tom"} 2 email = user.get(_______) ^ 3 print(email.strip())
Traceback (most recent call last):
File "main.py", line 3, in <module>
print(email.strip())
^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'strip'
空欄を埋めて実行を通す
dict.get()は該当キーが無いとNoneを返します(例外にはなりません)。存在確認をしないままメソッドを呼ぶと、ここでエラーになります。
次の問題