One of the things you will always encounter when doing ** programming ** is ** error **. When I ran the program I wrote so hard, I got a long sentence in English that I didn't understand! I think that many people are frustrated because this error cannot be resolved. In this article, I'll tell you about the causes of errors in ** Python ** and how to resolve them.
)
or forget to write the colon :
in the double-byte space, ʻif statement,
forstatement, and
while` statement.
From here, I will explain with some examples.** Parentheses ** If you don't have ()
or forget to close it,
SyntaxError: unexpected EOF while parsing
I get the error.
hello_world1.py
print("Hello, world!"
# SyntaxError: unexpected EOF while parsing
In this code, I forgot to close the parentheses, so I get the error "** There is an unexpected end of the sentence **". Close the parentheses to eliminate the error.
hello_world2.py
print("Hello, world!")
# Hello, world!
If ** double-byte space ** is mixed in the code,
SyntaxError: invalid character in identifier
I get the error.
circle1.py
r = 2
pi = 3.14
# ^It's hard to understand, but there is a double-byte space here.(The red underline is displayed)
print(r ** 2 * 3.14)
# SyntaxError: invalid character in identifier
Double-byte spaces are hard to find, but let's find the number of lines line (number)
displayed in the error as a hint and correct it.
circle2.py
r = 2
pi = 3.14
print(r ** 2 * 3.14)
# 12.56
If you forget to add ** colon ** :
to the end of a line such as ʻif statement,
forstatement,
while statement, **
SyntaxError: invalid syntax`**
I get the error.
ten1.py
i = 10
if i == 10
print("i is 10")
else
print("i is not 10")
# SyntaxError: invalid syntax
ʻIf, ʻelse
Add a colon to the end of each sentence to solve the problem.
ten2.py
i = 10
if i == 10:
print("i is 10")
else:
print("i is not 10")
#i is 10
If the called variable is undefined or misspelled,
** NameError: name'variable name' is not defined
**
I get the error.
year1.py
year = 2020
print(Year)
print(month)
# NameError: name 'Year' is not defined
Be careful of lowercase and uppercase letters. Since the program is executed from above, it is not mentioned that the variable month
is not defined, but this also causes an error.
year2.py
year = 2020
print(year)
# 2020
month = 3
print(month)
# 3
str ()
or ʻint () `to convert.When performing calculations or combining strings by assigning to print ()
or variables, the string type str
and the numeric type ʻintcannot be used at the same time, and if you try to use them **
TypeError: Can't convert'Type 1'object to (Type 2) implicitly` **
I get the error.
price1.py
price = 100
print("This product is" + price + "It is a circle.")
# TypeError: Can't convert 'int' object to str implicitly
Since 100
, which is of type ʻint, is assigned to
price, it cannot be combined with type
str. In such a case, use the
str ()function to convert
price to
strtype. Similarly, if you want to convert a
str type to a ʻint
type in a numerical calculation, use the ʻint ()` function.
price2.py
price = 100
print("This product is" + str(price) + "It is a circle.")
#This product is 100 yen.
The same is true for the arguments of other functions such as len ()
.
is processed in the indentation of the ʻif
statement.If there is any mistake in the indent,
IndentationError: unindent does not match any outer indentation level
I get the error.
There is only one workaround for this error. "** Check indentation **". Check if the tab
key and two half-width spaces are mixed.
nine1.py
number = 99999
if number % 9 == 0:
print("number is a multiple of 9.")
else:
print("number is not divisible by 9.")
# ^^There are two half-width spaces here.
# IndentationError: unindent does not match any outer indentation level
Make sure you have one indent. Also, let's get ʻelse` out.
nine2.py
number = 99999
if number % 9 == 0:
print("number is a multiple of 9.")
else:
print("number is not divisible by 9.")
#number is a multiple of 9.
0
** ".When specifying an element of list
or str
, if you specify an index number that does not exist
** ʻIndexError: (type) index out of range` **
I get the error.
abc1.py
l = ["a","b","c"]
print(l[3])
# IndexError: list index out of range
The index number of the third element will be 2
instead of 3
. Note that the index number of the first element is 0
.
The rightmost element can also be specified with -1
.
abc2.py
l = ["a","b","c"]
print(l[2])
# c
If you call a module that does not exist (or the object specified by from import
),
ʻImportError: No module named'specified module'`
I get the error.
math1.py
import maths
print(maths.sqrt(2))
# ImportError: No module named 'maths'
The maths
module does not exist. Let's rewrite it as math
.
math2.py
import math
print(math.sqrt(2))
# 1.4142135623730951
When I ran the error code
in the Python 3.8.2
environment,
ModuleNotFoundError: No module named 'maths'
I got an error message. The above error was displayed in Python3.4.3
, so it may be different depending on the version, but I don't think there is any particular problem.
Recommended Posts