LoginSignup
3
9

More than 3 years have passed since last update.

初心者に多いPythonエラー 10選

Posted at

初めてPythonを学ぶ途端、Pythonのエラーメッセージの意味を理解するのは少し複雑かもしれません。よく見るエラーヒントは下記のようにご参照ください。

1)if、elif、else、for、while、class、defの最後に追加するのを忘れた。

エラーヒント:SyntaxError :invalid syntax
エラーは次のコードで発生します。
1.png

2)「=」と「==」の使用

エラーヒント:SyntaxError :invalid syntax
「=」は代入演算子であり、「==」は比較演算である。エラーは次のコードで発生します。
2.png

3)forループでlen()を呼び出すのを忘れた

エラーヒント:TypeError: ‘list’ object cannot be interpreted as an integer
通常、インデックスでlistまたはstringの要素を反復処理します。これには、range()関数を呼び出す必要があります。 このリストを返す代わりに、len値を返すことを忘れないでください。エラーは次のコードで発生します。
3.png

4)string値を変更する

エラーヒント:TypeError: ‘str’ object does not support item assignment
stringは不変のデータ型であり、エラーは次のコードで発生します。
4.png

5)文字列以外の値を文字列に接続する

エラーヒント:TypeError: Can’t convert ‘int’ object to str implicitly
エラーは次のコードで発生します。
5.png

6)文字列の最初と最後に「’」を追加するのを忘れた

エラーヒント:SyntaxError: EOL while scanning string literal
エラー次のコードで発生します。
6.png

7)変数名としてPythonキーワードを使用する

エラーヒント:SyntaxError: EOL while scanning string literal
Pythonキーワードは変数名として使用できません。このエラーは次のコードで発生します:
7.png

8)メソッド名のスペルが間違っている

エラーヒント:AttributeError: ‘str’ object has no attribute ‘lowerr’
このエラーは次のコードで発生します:
8.png

9)参照がリストの最大インデックスを超えている

エラーヒント:IndexError: list index out of range
このエラーは次のコードで発生します:
9.png

10)range()を使用して整数のリストを作成する

range()は実際にlist値ではなく「range object」を返すことを覚えておく必要があります。
エラーヒント:TypeError: ‘range’ object does not support item assignment
このエラーは次のコードで発生します:
10.png

3
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
9