程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Types of Python statement errors

編輯:Python
# 1. When splicing with a plus sign , You must use a string .
'''
name=' Shinsuke harano '
age=5
print(' My name is '+name+', I this year '+age+' Year old ')
# TypeError: can only concatenate str (not "int") to str
An error will be reported when clicking run output , The error message is :TypeError: must be str, not int, Translation to wrong type , Must be a string str, It can't be a number int.
'''
# Solution :
name=' Shinsuke harano '
age='5'
print(' My name is '+name+', I this year '+age+' Year old ')
name=' Big pineapple '
age=18
print(' My name is '+name+', I this year '+str(age)+' Year old ')
# 2. Grammar mistakes .
'''
name=' Xiao Ming ,' Xiaolan ''
print(name)
# SyntaxError: invalid syntax
Click Run to report an error , The error message is :SyntaxError: invalid syntax , Grammar error : Invalid grammar
'''
# Solution :
name=" Xiaoxin ,' Nini ', Wind room , due south , Adai "
print(name)
# 3. An index error occurred while using the list .
'''
list=[9,8,7,6,5,4]
print(list[7])
# IndexError: list index out of range
Click on the run , The error message is :IndexError: list index out of range, Indication error : The index value of the list is out of range .
'''
# Solution , Change index value .
list=['《 Crayon Shin Chan 》',8,7,6,5,4]
print(list[0])
# 4. Property error .
'''
tp1=('1','2','3','4','5')
tp1.remove()
# AttributeError: 'tuple' object has no attribute 'remove'
Click on the run , The error message is :AttributeError: 'tuple' object has no attribute 'remove', Attribute error : There are no attributes in the tuple object remove. The solution is , Operate on attributes contained in tuples .
'''
# The solution is , Operate on attributes contained in tuples .
# 5. Wrong dictionary type .
'''
dic={'name':' Xiao Ming ','age':18,'fond':' To play basketball '}
dic.pop()
# TypeError: pop expected at least 1 argument, got 0
Click on the run , The error message is :TypeError: pop expected at least 1 arguments, got 0, Translation to wrong type :pop Expect at least one parameter , But now the parameter is 0. The solution is , stay pop Add an object you want to delete .
'''
# The solution is , stay pop Add an object you want to delete .
dic={'name':' Cai Xukun: A boy devoted to music in "More than Forever" ','age':20,'fond':' To play basketball '}
dic.pop('name')
print(dic)
# 6. Dictionary keyword error .
'''
dic={'name':' Big pineapple ','age':20,'fond':' Chasing stars '}
print(dic['height'])
# KeyError: 'height'
Click on the run , The error message is :KeyError: 'height', Translation to keyword error :height.
'''
# The solution is to change the keywords contained in the dictionary .
dic={'name':' Big pineapple ','age':20,'fond':' Chasing stars '}
print(dic['fond'])
# 7. Wrong string value .
'''
info='Hello World'
result=info.index('a')
print(result)
# ValueError: substring not found
Click on the run , The error message is :ValueError: substring not found, Value error : Substring not found .
'''
# Solution : To replace the missing substring with the substring owned in the string .
info='Hello World'
result=info.index('H') #index() Find where the string first appears
print(result)
# 8. String index error .
'''
info='Hello World'
print(info[20])
# IndexError: string index out of range
Click on the run , The error message is :IndexError: string index out of range, Indication error : The string index is out of range .
'''
# Solution : To view the length of the string , Replace index value , To be less than the length of this string .
info='Hello World'
print(info[0])
# 9.while Circular error messages .
'''
while True:
count+=1
if count==20:
return
# SyntaxError: 'return' outside function
Click on the run , The error message is :SyntaxError: 'return' outside function, Grammar error :‘ return ’ Other functions .
'''
# 10. The indentation error
'''
x=10
if x <10:
print(' This number is less than 10')
else:
print(' This number is greater than 10')
# IndentationError: unindent does not match any outer indentation level
Click on the run , The error message is :IndentationError: unindent does not match any outer indentation level, Indentation error : Position indent does not match any indent level
'''
# terms of settlement : In the process of writing code , Don't use it tab Use the space bar when pressing , Same use tab Key to indent .
x=10
if x <10:
print(' This number is less than 10')
else:
print(' This number is greater than 10')
# 11. The interpreter prompts such as :SyntaxError: invalid character in identifier
# But I still can't find the problem , Please make sure that there are no Chinese spaces in the code line ,tab etc. , Non literal character .
'''
Solution : Maybe it's an inexplicable mistake , You can try to rewrite the code copy Create a new one and run it again . Just copy it in !
'''


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved