When I first learned Python, it may be a bit complicated to understand the meaning of the Python error message. Some common running errors that make your program crash appear here.
1) if, elif, else, for, while, class, and def are not added at the end of the declaration, "SyntaxError: invalid syntax" is returned ")
This error will occur in code similar to the following:
- if spam == 42
- print('Hello!')
2) use = instead of = to cause "SyntaxError: invalid syntax ")
= Is the value assignment operator and = is a comparison operation. This error occurs in the following code:
- if spam = 42:
- print('Hello!')
3) The number of indentions is incorrect. "IndentationError: unexpected indent", "IndentationError: unindent does not match any outer indetation level", and "IndentationError: expected an indented block ")
Remember to add indentation only after the statement ending with:, and then it must be restored to the original indent format. This error occurs in the following code:
- Print ('Hello! ')
- Print ('Howdy! ')
- Or:
- If spam = 42:
- Print ('Hello! ')
- Print ('Howdy! ')
- Or:
- If spam = 42:
- Print ('Hello! ')
4) forget to call len () in the for loop Statement (resulting in "TypeError: 'LIST' object cannot be interpreted as an integer ")
Generally, You Want To iterate a list or string element through an index. You need to call the range () function. Remember to return the len value instead of the list.
This error occurs in the following code:
- spam = ['cat', 'dog', 'mouse']
- for i in range(spam):
- print(spam[i])
5) try to modify the string value and cause "TypeError: 'str' object does not support item assignment ")
String is an unchangeable data type. This error occurs in the following code:
- spam = 'I have a pet cat.'
- spam[13] = 'r'
- print(spam)
But you actually want to do this:
- spam = 'I have a pet cat.'
- spam = spam[:13] + 'r' + spam[14:]
- print(spam)
6) try to connect a non-string value to the string and cause "TypeError: Can't convert 'int' object to str implicitly ")
This error occurs in the following code:
- numEggs = 12
- print('I have ' + numEggs + ' eggs.')
But you actually want to do this:
- NumEggs = 12
- Print ('I have' + str (numEggs) + 'eggs .')
- Or:
- NumEggs = 12
- Print ('I have % s eggs.' % (numEggs ))
7) if you forget to add quotation marks at the beginning and end of the string, the following error occurs: "SyntaxError: EOL while scanning string literal ")
This error occurs in the following code:
- Print (Hello! ')
- Or:
- Print ('Hello !)
- Or:
- MyName = 'al'
- Print ('My name is '+ myName +. How are you? ')
8) the spelling of variables or function names leads to "NameError: name 'fooba' is not defined ")
- Foobar = 'al'
- Print ('My name is '+ fooba)
- Or:
- Spam = ruond (4.2)
- Or:
- Spam = Round (4.2)
9) "AttributeError: 'str' object has no attribute 'lowerr'" caused by incorrect method name spelling '")
- spam = 'THIS IS IN LOWERCASE.'
- spam = spam.lowerr()
10) the reference exceeds the maximum list index, causing "IndexError: list index out of range ")
This error occurs in the following code:
- spam = ['cat', 'dog', 'mouse']
- print(spam[6])