In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "what are the mistakes when writing Python". In the daily operation, I believe that many people have doubts about what errors there are when writing Python. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the questions of "what are the mistakes in the compilation of Python?" Next, please follow the editor to study!
one
Forgot to add the following at the end of statements such as if,for,def,elif,else,class:
Will result in "SyntaxError: invalid syntax" as follows:
If spam = = 42
Print ('Hellobirds')
two
Use = instead of =
It also leads to "SyntaxError: invalid syntax".
= is the assignment operator and = = is equal to the comparison operation. The error occurs in the following code:
If spam = 42:
Print ('Hellobirds')
three
Incorrect use of indentation
Lead to "IndentationError:unexpected indent", "IndentationError:unindent does not match any outer indetation level" and "IndentationError:expected an indented block"
Remember that indentation increments are used only after statements that end with:, and then you must revert to the previous indentation format. The error occurs in the following code:
Print ('Hellobirds')
Print ('howdyboy')
Or:
If spam = = 42:
Print ('Hellobirds')
Print ('howdy') 4
Forgot to call len () in the for loop statement
Lead to "TypeError: 'list' object cannot be interpreted as an integer"
Usually you want to iterate over an list or string element through an index, which requires a call to the range () function. Remember to return the len value instead of the list.
The error occurs in the following code:
Spam = ['cat',' dog', 'mouse']
For i in range (spam):
Print (SPAM [I]) 5
Try to change the value of string
Lead to "TypeError: 'str' object does not support item assignment"
String is an immutable data type, and the error occurs in the following code:
Spam ='I have a pet cat.'
Spam [13] ='r'
Print (spam)
And the right thing to do is:
Spam ='I have a pet cat.'
Spam = spam [: 13] +'r' + spam [14:]
Print (spam) 6
Try to connect a non-string value to a string
Lead to "TypeError: Can't convert 'int' object to str implicitly"
The error occurs in the following code:
NumEggs = 12
Print ('I have'+ numEggs + 'eggs.')
And the right thing to do is:
NumEggs = 12
Print ('I have'+ str (numEggs) + 'eggs.')
NumEggs = 12
Print ('I have% s eggs.'% (numEggs)) 7
Forget to put quotation marks at the beginning and end of the string
Lead to "SyntaxError: EOL while scanning string literal"
The error occurs in the following code:
Print (hellograms')
Print ('Hello!)
MyName = 'Al'
Print ('My name is'+ myName +. How are you?')
eight
Misspelling of variable or function name
Lead to "NameError: name 'fooba' is not defined"
The error occurs in the following code:
Foobar = 'Al'
Print ('My name is'+ fooba)
Spam = ruond (4.2)
Spam = Round (4.2) 9
Method name is misspelled
Lead to "AttributeError: 'str' object has no attribute' lowerr'"
The error occurs in the following code:
Spam = 'THIS IS IN LOWERCASE.'
Spam = spam.lowerr ()
ten
The reference exceeds the list maximum index
Lead to "IndexError: list index out of range"
The error occurs in the following code:
Spam = ['cat',' dog', 'mouse']
Print (spam [6]) 11
Use dictionary key values that do not exist
Lead to "KeyError:'spam'"
The error occurs in the following code:
Spam = {'cat':' Zophie', 'dog':' Basil', 'mouse':' Whiskers'}
Print ('The name of my pet zebra is' + spam ['zebra']) 12
Try using the Python keyword as the variable name
Lead to "SyntaxError:invalid syntax"
The Python key cannot be used as a variable name, and the error occurs in the following code:
Class = 'algebra'
Python3 keywords are: and, as, assert, break, class, continue, def, del, elif, else, except, False, finally, for, from, global, if, import, in, is, lambda, None, nonlocal, not, or, pass, raise, return, True, try, while, with, yield
thirteen
Use the value-added operator in a new variable definition
Lead to "NameError: name 'foobar' is not defined"
Do not use 0 or an empty string as the initial value when declaring variables, so a sentence using the self-increment operator spam + = 1 equals spam = spam + 1, which means that spam needs to specify a valid initial value.
The error occurs in the following code:
Spam = 0
Spam + = 42
Eggs + = 4214
Use a local variable in a function before defining a local variable (there is a global variable with the same name as the local variable)
Lead to "UnboundLocalError: local variable 'foobar' referenced before assignment"
It is complicated to use a local variable in a function while there is a global variable of the same name. The rule is: if anything is defined in the function, if it is only used in the function, it is local, and vice versa.
This means that you cannot use it as a global variable in a function before defining it.
The error occurs in the following code:
SomeVar = 42
Def myFunction ():
Print (someVar)
SomeVar = 100
MyFunction () 15
Try to create a list of integers using range ()
Lead to "TypeError: 'range' object does not support item assignment"
Sometimes you want to get an ordered list of integers, so range () seems like a good way to generate this list. However, you need to remember that range () returns "range object", not the actual list value.
The error occurs in the following code:
Spam = range (10)
Spam [4] =-1
Write it correctly:
Spam = list (range (10))
Spam [4] =-1
(note: spam = range (10) works in Python 2, because range () returns a list value in Python 2, but the above error occurs in Python 3.)
sixteen
There is no + + or-- self-increasing and self-subtracting operator.
Lead to "SyntaxError: invalid syntax"
If you are used to other languages such as C++, Java, PHP, etc., you may want to try using + or-- add and minus a variable. There is no such operator in Python.
The error occurs in the following code:
Spam = 1
Spam++
Write it correctly:
Spam = 1
Spam + = 117
Forgot to add the self parameter to the first parameter of the method
Cause "TypeError: myMethod () takes no arguments (1 given)"
The error occurs in the following code:
Class Foo ():
Def myMethod ():
Print ('Hellobirds')
A = Foo ()
A.myMethod () at this point, the study of "what are the mistakes in writing Python" is over. I hope I can solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.