What are the common errors that Python beginners have?

Source: Internet
Author: User

For beginners, beginner python, will always encounter such an error, want to understand the meaning of Python errors may not know how to do, here are some of the more common Python error problems, I hope to learn python can help some people.

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

This error occurs in the following code:

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

And you actually want to do this:

Numeggs = 12print (' I have ' + str (numeggs) + ' eggs. ')
Or:
Numeggs = 12print (' I have%s eggs. '% (Numeggs))

2 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:

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

3 ) variable or function name spelling error (resulting in "nameerror:name ' Fooba 'is not defined")

This error occurs in the following code:

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

4 The method name is misspelled (resulting in "attributeerror: ' str ' object has no attribute ' Lowerr')

This error occurs in the following code:

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

5 ) reference exceeds the list maximum index (resulting in "Indexerror:list indexout of range")

This error occurs in the following code:

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

6 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 ')

7 forget to declare in if, elif , else , for , while, class ,def Add at the end: (causes "syntaxerror : Invalid syntax")

The error will occur in code similar to the following:

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

8) 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 = 42:
Print (' hello! ')

9 ) uses the wrong indent amount. (causes "indentationerror: Unexpected indent", "indentationerror: Unindent does not match Any outer indetationlevel "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: if spam = = 42:
Print (' hello! ')
Print (' howdy! ')
Or: if spam = = 42:
Print (' hello! ')

Ten ) in the FOR Loop statement, forget to call Len ()(resulting in "TypeError: ' List ' object cannot is 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 I in range (spam):
Print (Spam[i])

python Some of the most important built-in exception class names Summary

Attributeerror: Property error, attribute reference and assignment failure when property error is raised

Nameerror: The variable name you are trying to access does not exist

SyntaxError: syntax error, code form error

Exception: The base class for all exceptions, because all Python exception classes are members of the base class Exception, and exceptions are inherited from the base class Exception and are defined in the Exceptions module.

IOError: It is generally common to throw IOError errors when opening files that do not exist, or to solve output input errors

Keyerror: A keyword error that is thrown when a keyword (key) that does not exist in the map is used

Indexerror: Index error, index not present, constant index out of sequence range, what is index

TypeError: Type error, built-in operation, or function should throw a type error on an object of the wrong type

Zerodivisonerror: The divisor is 0, and the second argument is 0 o'clock when it is used in a division operation, the error is raised

ValueError: A value error, the argument type passed to the object is not correct, like a parameter that passed the string data type to the Int () function.

Hope that the above summary, to everyone can have some help, if there is no place, but also hope that a lot of advice, mutual exchange and progress.

What are the common errors that Python beginners have?

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.