Beginner Python common errors
- Colon forget to write
- Misuse =
- Error tightening
- Variable is not defined
- Errors in text input
- Different types of data splicing
- Index location problem
- Use the key does not exist in the dictionary
- Forget the brackets
- Leakage pass parameters
- Missing dependencies
- Use of keywords in python
- Coding problems
1. forget to write a colon
Forget to add in the back if, elif, else, for, while, def statement :
age = 42
if age == 42
print('Hello!')
File "<ipython-input-19-4303141d6f97>", line 2
if age == 42
^
SyntaxError: invalid syntax
2. Misuse =
'''
遇到问题没人解答?小编创建了一个Python学习交流QQ群:××× 寻找有志同道合的小伙伴,
互帮互助,群里还有不错的视频学习教程和PDF电子书!
'''
=` 是赋值操作,而判断两个值是否相等是 `==
gender = '男'
if gender = '男':
print('Man')
File "<ipython-input-20-191d01f95984>", line 2
if gender = '男':
^
SyntaxError: invalid syntax
3. Error indentation
Python distinguish between code block, common errors with usage indent:
print('Hello!')
print('Howdy!')
File "<ipython-input-9-784bdb6e1df5>", line 2
print('Howdy!')
^
IndentationError: unexpected indent
num = 25
if num == 25:
print('Hello!')
File "<ipython-input-21-8e4debcdf119>", line 3
print('Hello!')
^
IndentationError: expected an indented block
4. The variable is not defined
'''
遇到问题没人解答?小编创建了一个Python学习交流QQ群:××× 寻找有志同道合的小伙伴,
互帮互助,群里还有不错的视频学习教程和PDF电子书!
'''
if city in ['New York', 'Bei Jing', 'Tokyo']:
print('This is a mega city')
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-22-a81fd2e7a0fd> in <module>
----> 1 if city in ['New York', 'Bei Jing', 'Tokyo']:
2 print('This is a mega city')
NameError: name 'city' is not defined
5. errors in text input
- Colon
- English brackets
- Comma
- English single and double quotation marks
if 5>3:
print('5比3大')
File "<ipython-input-46-47f8b985b82d>", line 1
if 5>3:
^
SyntaxError: invalid character in identifier
if 5>3:
print('5比3大')
File "<ipython-input-47-4b1df4694a8d>", line 2
print('5比3大')
^
SyntaxError: invalid character in identifier
spam = [1, 2,3]
File "<ipython-input-45-47a5de07f212>", line 1
spam = [1, 2,3]
^
SyntaxError: invalid character in identifier
if 5>3:
print('5比3大‘)
File "<ipython-input-48-ae599f12badb>", line 2
print('5比3大‘)
^
SyntaxError: EOL while scanning string literal
6. The different data types splicing
String / list / tuple support splicing
Dictionary / collection does not support splicing
'''
遇到问题没人解答?小编创建了一个Python学习交流QQ群:××× 寻找有志同道合的小伙伴,
互帮互助,群里还有不错的视频学习教程和PDF电子书!
'''
'I have ' + 12 + ' eggs.'
#'I have {} eggs.'.format(12)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-29-20c7c89a2ec6> in <module>
----> 1 'I have ' + 12 + ' eggs.'
TypeError: can only concatenate str (not "int") to str
['a', 'b', 'c']+'def'
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-31-0e8919333d6b> in <module>
----> 1 ['a', 'b', 'c']+'def'
TypeError: can only concatenate list (not "str") to list
('a', 'b', 'c')+['a', 'b', 'c']
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-33-90742621216d> in <module>
----> 1 ('a', 'b', 'c')+['a', 'b', 'c']
TypeError: can only concatenate tuple (not "list") to tuple
set(['a', 'b', 'c'])+set(['d', 'e'])
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-35-ddf5fb1e6c8c> in <module>
----> 1 set(['a', 'b', 'c'])+set(['d', 'e'])
TypeError: unsupported operand type(s) for +: 'set' and 'set'
grades1 = {'Mary':99, 'Henry':77}
grades2 = {'David':88, 'Unique':89}
grades1+grades2
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-36-1b1456844331> in <module>
2 grades2 = {'David':88, 'Unique':89}
3
----> 4 grades1+grades2
TypeError: unsupported operand type(s) for +: 'dict' and 'dict'
7. The index location of the problem
spam = ['cat', 'dog', 'mouse']
print(spam[5])
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-38-e0a79346266d> in <module>
1 spam = ['cat', 'dog', 'mouse']
----> 2 print(spam[5])
IndexError: list index out of range
8. key does not exist in the dictionary
Access key in the dictionary objects can be used []
,
But if the key does not exist, it will lead to: KeyError: 'zebra'
'''
遇到问题没人解答?小编创建了一个Python学习交流QQ群:××× 寻找有志同道合的小伙伴,
互帮互助,群里还有不错的视频学习教程和PDF电子书!
'''
spam = {'cat': 'Zophie',
'dog': 'Basil',
'mouse': 'Whiskers'}
print(spam['zebra'])
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
<ipython-input-39-92c9b44ff034> in <module>
3 'mouse': 'Whiskers'}
4
----> 5 print(spam['zebra'])
KeyError: 'zebra'
To avoid this, a method can use the get
spam = {'cat': 'Zophie',
'dog': 'Basil',
'mouse': 'Whiskers'}
print(spam.get('zebra'))
None
When the key does not exist, get the default return None
9. forget the brackets
When the function is passed in function or method, it is easy to write leak brackets
'''
遇到问题没人解答?小编创建了一个Python学习交流QQ群:××× 寻找有志同道合的小伙伴,
互帮互助,群里还有不错的视频学习教程和PDF电子书!
'''
spam = {'cat': 'Zophie',
'dog': 'Basil',
'mouse': 'Whiskers'}
print(spam.get('zebra')
File "<ipython-input-43-100a51a7b630>", line 5
print(spam.get('zebra')
^
SyntaxError: unexpected EOF while parsing
10. The drain-parameters
def diyadd(x, y, z):
return x+y+z
diyadd(1, 2)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-44-7184f3f906ca> in <module>
2 return x+y+z
3
----> 4 diyadd(1, 2)
TypeError: diyadd() missing 1 required positional argument: 'z'
11. The lack of dependent libraries
No computer library
12. Use of the Image python
如try、except、def、class、object、None、True、False等
'''
遇到问题没人解答?小编创建了一个Python学习交流QQ群:××× 寻找有志同道合的小伙伴,
互帮互助,群里还有不错的视频学习教程和PDF电子书!
'''
try = 5
print(try)
File "<ipython-input-1-508e87fe2ff3>", line 1
try = 5
^
SyntaxError: invalid syntax
def = 6
print(6)
File "<ipython-input-2-d04205303265>", line 1
def = 6
^
SyntaxError: invalid syntax
13. The file encoding problem
import pandas as pd
df = pd.read_csv('data/twitter情感分析数据集.csv')
df.head()
Try encoding encoding parameters passed in utf-8, gbk
df = pd.read_csv('data/twitter情感分析数据集.csv', encoding='utf-8')
df.head()
Are being given instructions and encoding is not utf-8 gbk, but are not common coding, where we need to pass both the right encoding, in order to let the program run.
There chardet python library, designed to detect coding.
import chardet
binary_data = open('data/twitter情感分析数据集.csv', 'rb').read()
chardet.detect(binary_data)
{'encoding': 'Windows-1252', 'confidence': 0.7291192008535122, 'language': ''