Although it is a memorandum for myself to learn Python, here is a summary of how to read error messages.
I would like to take this opportunity to thank you for solving the problem by borrowing the wisdom of our predecessors, and I am very sorry to say that I will summarize it here as my own memo.
1-1.
--Caused by miss typing. It also happens with the difference between uppercase and lowercase letters.
python
prlnt("Hello World !") #Wrong...print→prlnt
print("Hello World !") #Positive
Terminal
Traceback (most resent call last):
file "c:/~/sample.py", line 1, in <module>
pilnt("Hello World !")
NameError: name 'Pilnt' is not defined
[Translation] Name error in module in last last call: The name "pilnt" on the first line is undefined. ↓ ** "I made a misspelling! 』** That means.
1-2.
--Occurs when a function call is made before the function definition.
python
print(cost(100000)) #Wrong...Calling cost before definition
def cost(bill):
return int(bill * 1.1)
Terminal
NameError: name 'cost' id not defined
[Translation] Name error occurred: "cost" is not defined. ↓ ** "I haven't figured out or understood the function" cost "yet" **.
――It happens due to a mistake in grammar.
2-1.
python
print("Hello World !) #Wrong...Forget closing quotes
print("Hello World !") #Positive
Terminal
SyntaxError: EOL while scaning stringliteral
[Translation] A syntax error occurred: An End Of Line was found while scanning a string literal. ↓ ** "I thought it was a character string, but it ended up at the end of the line! ?? Fine? 』** That means.
2-2.
python
print( Hello World ! ) #Wrong...Forget quotes
print("Hello World !") #Positive
Terminal
SyntaxError: invalid character in identifier
[Translation] A syntax error has occurred: An invalid character was found in the identifier. ↓ ** "I thought it was a variable or a function (because it's not enclosed in quotes), but don't use"! "~" **.
Note
The "!" (Exclamation mark) is a character that cannot be used as the name of a variable or function. For this reason, it has been pointed out as an illegal character.
2-3.
python
ifheight > 180: #Wrong...There is no half-width space immediately after if
if height > 180: #Positive
Terminal
SyntaxError: invalid systax
[Translation] Grammar error occurred: Illegal grammar. ↓ ** "You can't write like that" **.
--Occurs when there is a problem with the data type.
python
print(5 + "Sheet") #Wrong...5(Numerical value)When,"Sheet"(String)I tried to combine with the + operator, but I can't concatenate because they are different.
print("5" + "Sheet") #Positive...Match the type to the str type
print(str(5) + "Sheet") #Positive...Same as above
Terminal
TypeError: unsupporter operand type(s) for +: 'int' and 'str'
[Translation] Type error occurs: The + operator does not support the type of the operand. ↓ ** "Since the data type is different between int and str, we can't process them together" **.
--This happens when there is no nested indentation for if, for statement, function, etc., or when there is a gap.
python
if counter > 10:
break #Wrong...No indentation
if counter > 10:
break #Positive
Terminal
IndentationError: expected an indented block
[Translation] Indent error occurs: Predicts that an indented block is needed. ↓ ** "I haven't indented, but I need indentation ~" **.
--This happens when a character string containing non-numeric characters is used as an argument of the int function.
--Occurs when an unsupported data type is passed to a function.
Terminal
ValueError: invalid literal for int() with base 10: 'xxxxxxx'
[Translation] Value error occurred: Illegal literal: string "xxxxxxx" for int function with argument base 10. ↓ ** "I'm putting a character string to convert in decimal! 』** That means.
--Occurs when the module cannot be imported. (Mainly due to typos)
python
import xxxxxxxx
Terminal
ModuleNotFoundError: No module named 'xxxxxxxx' #I can't find the module name "xxxxxxxx"
[Translation] Module not found error occurred: There is no module named "xxxxxxxx". ↓ ** "There is no module named" xxxxxxxx "! 』** That means.
--What happens when you divide by so-called "zero"
python
10 + 10 + 10 + 10 * 0 + 10 + 10 / 0 + 10
Terminal
ZeroDivisionError: division by zero
[Translation] Zero division error occurred: Divided by zero. ↓ (As it is) ** "It's a zero split! 』** That means.
--It happens when you stop the infinite loop by pressing "ctrl" + "C" keys.
Terminal
Traceback (most resent call last):
file "c:/~/sample.py", line 120, in <module>
counter += 1
KeyboardInterruput
[Translation] I got an interrupt from the keyboard. ↓ ** "I stopped the program by inputting an interrupt from the keyboard! 』** That means.
I have collected and summarized the errors that I often encounter. First of all, I want to understand the content, cause and reason of a simple error so that I will not panic when an error occurs when programming complicatedly.
VS Code seems to have an extension that checks the grammar in real time, so I'll try various things.
Recommended Posts