python类型错误如何解决_在Python中 出现的错误类型以及解决办法
#IndentationError:expected an indented #block 縮進錯誤
#解決辦法 tab 縮進
for index in range(10):
if name == '小王':
print('nothing')
else:
print('nothing')
name ='小王'
age = 16
print('我的名字是'+name+'我的年齡是'+age)
#TypeError:must be str,not int
#類型錯誤
#解決辦法 使用拼接的時候 必須是字符串 或#者數字轉換成字符串
count = 0
while True:
count+=1
if count ==20:
return
#SyntaxError:'return'outside function
#return 不能在方法以外使用
#解決辦法:把 return放在方法以內使用
content = 'hello world'
result = content.index('r')
print(result)
#ValueError:substring not found
#值錯誤 子字符串未找到
content ='hello world'
print(content[21])
#IndexError:string index out of range
#索引錯誤 字符串的長度超出了范圍
#解決辦法 查看字符串的長度 索引要小于長度
list1 = ['autman','小李子','諾蘭','皮克斯']
print(list[5])
#TypeError:'list'object is not callable
#索引錯誤 列表索引超出范圍
tp1 = ((),[],{},1,a,3.14,True)
tp1.remove(1)
#AttributeError:'tuple'object has no attribure 'remove'
#屬性錯誤 元組對象沒有屬性'remove'
dic1 = {
'name':'張三',
'age':17,
'friend':['李四','王五','趙六']
}
#KeyError:'fond'
#key 鍵錯誤 沒有指定的鍵值'fond'
print(dic1['fond'])
dic1.pop()
#TypeError:pop expected at least 1 argument,got 0
#argument 參數 expected 期望 at least 至少
#類型錯誤:pop方法希望得到至少一個參數 但是現在參數為0
date = date1.strftime('%y 年%m月 %d日 %H時 %M分 %S秒')
# UnicodeEncodeError: 'locale' codec #can't encode character '\u5e74' in #position 3: Illegal byte sequence
# 編碼錯誤:本地文件不能對指定位置的字符進行編碼
#解決方法:用replace進行代替
date2 = date2.replace('year','年').replace('month','月').replace('day','日')
print(date2)
總結
以上是生活随笔為你收集整理的python类型错误如何解决_在Python中 出现的错误类型以及解决办法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 扫描到电脑上如何利用电脑扫描
- 下一篇: 打开一个窗口都需要很长的时间打开一个窗口