Beginner common Python run-time errors

Source: Internet
Author: User

1) Forget to add at the end of the IF, elif, else, for, while, class, Def declarations: (resulting in "syntaxerror:invalid syntax")

The error will occur in code similar to the following:

if spam = =    print('hello! ')

2) Use = instead of = = (causes "syntaxerror:invalid syntax")

= is an assignment operator and = = is equal to the comparison operation. This error occurs in the following code:

if spam =:    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:

Print('hello!')    Print('howdy!') or:ifSpam = = 42:    Print('hello!')  Print('howdy!') or:ifSpam = = 42:Print('hello!')

4) Forget to call Len () in the FOR Loop statement (resulting in "TypeError: ' List ' object cannot be interpreted as an integer")

Usually you want to iterate over an element of a list or string by index, which calls the range () function. Remember to return the Len value instead of returning the list.

This error occurs in the following code:

Spam = ['cat'dog'mouse']   for in range (spam):    print(Spam[i])

5) Attempt to modify the value of string (resulting in "TypeError: ' str ' object does not the support item assignment")

A string is an immutable data type that occurs in the following code:

' I have a pet cat. ' spam['r'print(spam)

And you actually want to do this:

' I have a pet cat. '  'r' + spam[14:]print(spam)

6) Attempt to concatenate non-string value with string (resulting in "Typeerror:can ' t convert ' int ' object to str implicitly")

This error occurs in the following code:

Numeggs =Print(" ' eggs. ')

And you actually want to do this:

Numeggs =Print(" ' eggs. '  =print('I have%s eggs. ' % (Numeggs))

7) Forget the quotes in the string (resulting in "syntaxerror:eol while scanning string literal")

This error occurs in the following code:

Print (hello! ' ) or:print('hello!)  'Al'print(' + 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:

' Al ' Print (' += Ruond (4.2= Round (4.2)

9) Method name spelling error (causes "Attributeerror: ' str ' object has no attribute ' Lowerr ')

This error occurs in the following code:

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

10) Reference exceeds list maximum index (resulting in "Indexerror:list index out of range")

This error occurs in the following code:

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:

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:

class ' algebra '

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

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

This error occurs in the following code:

Spam =+ ++ + + 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:

Somevar =def  myFunction ():    print(somevar)    =myFunction () 

15) Try using range () to create a list of integers (resulting in "TypeError: ' Range ' object does not support item assignment")

Sometimes you want to get an ordered list of integers, so range () looks like a good way to generate this list. However, you need to remember that range () returns "Range object" instead of the actual list value.

This error occurs in the following code:

Spam = range (ten) spam[4] = 1

Maybe that's what you want to do:

Spam = List (range) spam[4] = 1

16) Good at + + or--self-increment self-decrement operator. (resulting in "syntaxerror:invalid syntax")

If you are accustomed to other languages such as C + +, Java, PHP, and so on, you might want to try using + + or--self-increment-subtract a variable. There is no such operator in Python.

This error occurs in the following code:

Spam = 1spam+ +

Maybe that's what you want to do:

Spam = 1+ = 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:

class Foo ():     def MyMethod ():         Print ('hello! '  = Foo () A.mymethod ()

Transfer from https://www.oschina.net/question/89964_62779

Beginner common Python run-time errors

Related Article

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.