SoFunction
Updated on 2024-12-20

Python common error solution summary (newbie rescue guide)

preamble

If you say what you are most afraid of when writing code, it is undoubtedly bugs. and for newbies, who are just getting into programming, while enjoying the fulfillment of writing code, they are often confused by all kinds of bugs. Today, we made an issue of Python common error reporting to share and save your code!

01 IndentationError

In Python, all code is organized by correct spaces. So, whether it's an extra space or a missing space, the whole code won't run and only returns an error function.

Python code follows the PEP8 whitespace specification, using 4 spaces per indentation level.

error message

a=1
b=2
if a<b:
print a

amendment

a=1
b=2
if a<b:
    print a

02Tab and space mixup (TabError)

This type of error is caused by coding with both tabs and spaces; the tab key is essentially a tab, not an indent. Since the width of the space represented by the tab varies from one text editor to another, it is recommended to use spaces.

03 SyntaxError

The causes of grammatical errors include the following three:

  • Invalid syntax (invalid syntax) Missing punctuation, mixing Chinese and English symbols, spelling mistakes, using keywords in variable or function names.
  • Invalid character in identifier (invalid character in identifier) There are unrecognizable characters in the code, check whether there are redundant characters or Chinese characters.
  • Checking for incomplete strings (EOL while scanning string litera) is in many cases due to inconsistent quotes on both sides of the string.

error message

print( 'hello', 'world') Reason for error: comma for Chinese comma

Error message: SyntaxError: invalid character inidentifier

result = (1024+(512*2)/128Error cause: parentheses not appearing in pairs

error message (computing):SyntaxError:unexpected EOF whileparsing

if name =="A"
print("hello")

Reason for error: Forgot to add a colon at the end of if/elif/else/while/for/def/class etc. statements

Error message: SyntaxError:invalid syntax

04 Variable Name Error (NameErro)

Variable name errors are the most common and frequently encountered type of built-in error reporting, often occurring in Python variable naming, if the variable can not be found to raise the NameError. about the rules of the variable name, you need to keep in mind the following:

  • Variable names can only contain letters, numbers, and underscores, and may not begin with a number;
  • Variable names cannot contain spaces, but you can use underscores to separate words within them;
  • Do not use Python keywords and function names as variable names, such as print;
  • Variable names should be both short and descriptive;
  • Use lowercase l and uppercase O with caution, as they can easily be mistaken for the numbers 1 and 0.

If there is a variable name error, you can check whether the variable is assigned a value, whether there is a case inconsistency or the variable name is written incorrectly, and fix it when you find it.

error message

message = "Hello!"
print(mesage)

Reason for error: misspelled variable name, misspelled massage as masge

error message (computing):NameError: name 'mesage' is not defined

05 IndexError

The index is the position of the item in the array or list, and this exception occurs when we try to access an element from a list or a tuple from an index that does not exist in the list.

For example, if there is a list of 10 elements with indexes between 0 and 9, an IndexError is generated if an attempt is made to access an element at index 10 or 11 or more.

error message

a = [1,2,3]
print(a[3])

Reason for error: The 4th index does not exist in list a. The index of the list is numbered from 0.

error message (computing):IndexError: string index out of range

06 KeyError

A KeyError error is triggered if the key does not exist when reading the key and value in the dictionary.

error message

d = {'a':1,'b':2}
print(d['f'])

Reason for error: key 'f' does not exist

Error message: KeyError: 'f'

07 TypeError

This error is raised when an incorrect or unsupported object type is used in a program. This error is also raised if an attempt is made to call an uncallable object or iterate through a non-iterative identifier.

error message

age=18
print("My age is"+age)

Reason: When using "+" for splicing, you must either use a string, or convert the number to a string using str().

error message (computing):TypeError:can only concatenate str(not"int")to str

08 AttributeError

Attribute errors are thrown when feature references and assignments fail.

The reason for this type of error is an attempt to access an unknown object property, in other words, the property of the corresponding object cannot be found. You can check that the constructor __init__() in the class is written correctly, with two underscores on the left and right sides.

For beginners, frequent bugs in the code does not mean that you are not learning well. If you look at a bug as a small monster in the game, then the process of eliminating the bug is not the process of upgrading?

summarize

to this article on the Python common error solution to this article, more related to Python common error solution content please search my previous posts or continue to browse the following related articles I hope you will support me more in the future!