17 common Python runtime errors for beginners (1)

Source: Internet
Author: User

When I first learned Python, it may be a bit complicated to understand the meaning of the Python error message. Some common running errors that make your program crash appear here.

1) if, elif, else, for, while, class, and def are not added at the end of the declaration, "SyntaxError: invalid syntax" is returned ")

This error will occur in code similar to the following:

 
 
  1. if spam == 42 
  2.     print('Hello!') 

2) use = instead of = to cause "SyntaxError: invalid syntax ")

= Is the value assignment operator and = is a comparison operation. This error occurs in the following code:

 
 
  1. if spam = 42:  
  2.     print('Hello!') 

3) The number of indentions is incorrect. "IndentationError: unexpected indent", "IndentationError: unindent does not match any outer indetation level", and "IndentationError: expected an indented block ")

Remember to add indentation only after the statement ending with:, and then it must be restored to the original indent format. This error occurs in the following code:

 
 
  1. Print ('Hello! ')
  2. Print ('Howdy! ')
  3. Or:
  4. If spam = 42:
  5. Print ('Hello! ')
  6. Print ('Howdy! ')
  7. Or:
  8. If spam = 42:
  9. Print ('Hello! ')

4) forget to call len () in the for loop Statement (resulting in "TypeError: 'LIST' object cannot be interpreted as an integer ")

Generally, You Want To iterate a list or string element through an index. You need to call the range () function. Remember to return the len value instead of the list.

This error occurs in the following code:

 
 
  1. spam = ['cat', 'dog', 'mouse']  
  2. for i in range(spam):  
  3.     print(spam[i]) 

5) try to modify the string value and cause "TypeError: 'str' object does not support item assignment ")

String is an unchangeable data type. This error occurs in the following code:

 
 
  1. spam = 'I have a pet cat.' 
  2. spam[13] = 'r' 
  3. print(spam) 

But you actually want to do this:

 
 
  1. spam = 'I have a pet cat.' 
  2. spam = spam[:13] + 'r' + spam[14:]  
  3. print(spam) 

6) try to connect a non-string value to the string and cause "TypeError: Can't convert 'int' object to str implicitly ")

This error occurs in the following code:

 
 
  1. numEggs = 12 
  2. print('I have ' + numEggs + ' eggs.') 

But you actually want to do this:

 
 
  1. NumEggs = 12
  2. Print ('I have' + str (numEggs) + 'eggs .')
  3. Or:
  4. NumEggs = 12
  5. Print ('I have % s eggs.' % (numEggs ))

7) if you forget to add quotation marks at the beginning and end of the string, the following error occurs: "SyntaxError: EOL while scanning string literal ")

This error occurs in the following code:

 
 
  1. Print (Hello! ')
  2. Or:
  3. Print ('Hello !)
  4. Or:
  5. MyName = 'al'
  6. Print ('My name is '+ myName +. How are you? ')

8) the spelling of variables or function names leads to "NameError: name 'fooba' is not defined ")

 
 
  1. Foobar = 'al'
  2. Print ('My name is '+ fooba)
  3. Or:
  4. Spam = ruond (4.2)
  5. Or:
  6. Spam = Round (4.2)

9) "AttributeError: 'str' object has no attribute 'lowerr'" caused by incorrect method name spelling '")

 
 
  1. spam = 'THIS IS IN LOWERCASE.' 
  2. spam = spam.lowerr() 

10) the reference exceeds the maximum list index, causing "IndexError: list index out of range ")

This error occurs in the following code:

 
 
  1. spam = ['cat', 'dog', 'mouse']  
  2. print(spam[6]) 


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.