A summary of common errors for 17 novice users running Python

Source: Internet
Author: User


1) Forget the
If, elif, else, for, while, class, def declarations are added at the end: (Causes
"Syntaxerror:invalid Syntax")
The error will occur in code similar to the following:
The code is as follows:


If spam
= = 42
Print (' hello! ')


2) Use =
rather than = = (resulting in "syntaxerror:invalid syntax")
=
is an assignment operator and = = is equal to the comparison operation. This error occurs in the following code:
The code is as follows:


If spam
= 42:
Print (' hello! ')


3) Incorrect use of indent amount. (resulting in "indentationerror:unexpected
Indent "," Indentationerror:unindent does not match any outer
Indetation level "and" indentationerror:expected an indented
Block ")
Remember that the indent increment is only used after the statement that ends with: and then must revert to the previous indent format. This error occurs in the following code:
The code is as follows:


Print (' hello! ')
Print (' howdy! ')
Or:
If spam
= = 42:
Print (' hello! ')
Print (' howdy! ')
Or:
If spam
= = 42:
Print (' hello! ')


4) in the For
The Loop statement forgets to call Len () (resulting in "TypeError: ' List ' object cannot be interpreted
As an integer ")
Usually you want to iterate over an element of list or string by index, which requires calling
The range () function. Remember to return the Len value instead of returning the list.
This error occurs in the following code:
The code is as follows:


Spam =
[' Cat ', ' dog ', ' mouse ']
For I
In range (spam):
Print (Spam[i])


5) Try to modify the value of string (resulting in "TypeError:
' Str ' object does not support item assignment ")
A string is an immutable data type that occurs in the following code:
The code is as follows:


Spam =
' I have a pet cat. '
SPAM[13] =
' R '
Print (spam)


And you actually want to do this:
The code is as follows:


Spam =
' I have a pet cat. '
Spam =
SPAM[:13] + ' r ' + spam[14:]
Print (spam)


6) Attempt to connect a non-string value to a string (causing
"Typeerror:can ' t convert ' int ' object to str
Implicitly ")
This error occurs in the following code:
The code is as follows:


Numeggs
= 12
Print (' I have ' +
Numeggs + ' eggs. ')


And you actually want to do this:
The code is as follows:


Numeggs
= 12
Print (' I have ' +
STR (NUMEGGS) + ' eggs. ')
Or:
Numeggs
= 12
Print (' I have%s
Eggs. '% (Numeggs))


7) Forget the quotation marks at the end of the string (resulting in "syntaxerror:
EOL while scanning string literal ")
This error occurs in the following code:
The code is as follows:


Print (hello! ')
Or:
Print (' hello!)
Or:
MyName
= ' Al '
Print (' My name is '
+ MyName +. How is it? ')


8) Incorrect spelling of variable or function name (resulting in "nameerror:
Name ' Fooba ' is not defined ")
This error occurs in the following code:
The code is as follows:


Foobar
= ' Al '
Print (' My name is '
+ Fooba)
Or:
Spam =
Ruond (4.2)
Or:
Spam =
Round (4.2)


9) Method name spelling error (causes
"Attributeerror: ' str ' object has no attribute
' Lowerr ')
This error occurs in the following code:
The code is as follows:


Spam =
' This was in lowercase. '
Spam =
Spam.lowerr ()


10) Reference exceeds the list maximum index (resulting in "indexerror:
List index out of range ")
This error occurs in the following code:
The code is as follows:


Spam =
[' Cat ', ' dog ', ' mouse ']
Print (Spam[6])


11) Use a non-existent dictionary key value (resulting in "keyerror: ' Spam '")
This error occurs in the following code:
The code is as follows:


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 (resulting in "syntaxerror:invalid
Syntax ")


Python key cannot be used as a variable name, and this error occurs in the following code:
The code is as follows:
class =
' Algebra '


The key words of Python3 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


13) Use the value-added operator in a new defined variable (resulting in the nameerror:
Name ' Foobar ' is not defined ")


Do not use 0 or an empty string as the initial value when declaring a variable, so use the spam of the self-increment operator
+ = 1 equals spam = spam + 1, which means that spam needs to specify a valid initial value.


This error occurs in the following code:
The code is as follows:


Spam =
0
Spam + =
42
Eggs + =
42


14) Use a local variable in the function before defining the local variable (there is a global variable with the same name as the local variable) (resulting in "unboundlocalerror:
Local variable ' foobar ' referenced before assignment ")
It is very complicated to use a local variable to that in a function and a global variable with the same name at the same time: if anything is defined in the function, it is local if it is used only in the function, and the other is the global variable.
This means that you cannot define it before you use it as a global variable in a function.
This error occurs in the following code:
The code is as follows:


Somevar
= 42
Def
MyFunction ():
Print (Somevar)
Somevar
= 100
MyFunction ()


15) Try to use
Range () creates a list of integers (resulting in "TypeError: ' Range ' object does not a support item
Assignment ")
Sometimes you want to get an ordered list of integers, so
Range () appears to be a good way to generate this list. However, you need to remember that range () returns "Range object" instead of the actual
The list value.
This error occurs in the following code:
The code is as follows:


Spam =
Range (10)
SPAM[4]
=-1


Maybe that's what you want to do:
The code is as follows:


Spam =
List (range (10))
SPAM[4]
=-1


(Note: in
Python 2 spam = range (10) is OK, because in Python 2 the range ()
The list value is returned, but the above error is generated in Python 3)
16) Good at
+ + or--self-increment decrement operator. (resulting in "syntaxerror:invalid syntax")
If you are accustomed to, for example, C + +, Java
, PHP and other languages, you might want to try to use + + or--
Self-increment decrement a variable. There is no such operator in Python.
This error occurs in the following code:
The code is as follows:


Spam =
1
spam++


Maybe that's what you want to do:
The code is as follows:
Spam =
1
Spam + =
1


17) Forget to add the self parameter for the first parameter of the method (resulting in "TypeError:
MyMethod () takes no arguments (1 given) ")
This error occurs in the following code:
The code is as follows:


Class
Foo ():
Def
MyMethod ():
Print (' hello! ')
A =
Foo ()
A.mymethod ()

A summary of common errors for 17 novice users running Python

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.