1.
name = '小王'
age = 16
print('我的名字是' + name + ',我的年齡是' + age)
錯誤提示:TypeError: must be str, not int
中譯:類型錯誤 必須是一個字符串 不能是數字
解決辦法:使用+拼接的時候 必須使用字符串,或者將數字轉化成字符串。
2.
if name == '小王'
print('Hello')
錯誤提示:SyntaxError: invalid syntax
中譯:語法錯誤 非法的語法
解決辦法:看報錯信息在第幾行 ,從這一行往上找錯誤
3.
for index in range(10):
if name == '小王':
print('hello')
else:
print('nothing')
錯誤提示:IndentationError: unindent does not match any outer indentation level
中譯:縮進錯誤 縮進與外部縮進級別不匹配
解決辦法:把多餘的縮進刪掉
4.
for index in range(10):
if name == '小王':
print('hello')
else:
print('nothing')
錯誤提示:IndentationError: expected an indented block
中譯:縮進錯誤:期望縮進代碼塊
解決辦法:用tab鍵讓if語句縮進;如果是多行代碼需要縮進,可以選中然後按tab鍵,會全部往後縮進。
5.
content = 'hello world'
print(content[21])
錯誤提示:IndexError: string index out of range
中譯:索引錯誤 字符串超出了範圍
解決辦法:查看字符串的長度,索引要小於長度
6.
tp1 = ((),[],{},1,2,3,'a','b','c',3.14 ,True)
tp1.remove(1)
print(tp1)
錯誤提示:AttributeError: 'tuple' object has no attribute 'remove'
中譯:屬性錯誤 元組對象沒有屬性'remove'
解決辦法:元組不能進行增、刪、改等操作
content = 'Hello World'
result = content.index('100')
print(result)
錯誤提示:ValueError: substring not found
中譯:值錯誤:子字符串未找到
解決辦法:這個方法如果找不到指定的字符串會報錯,用的時候要注意
8.dic1 = {
'name':'張三',
'age':17,
'friend':['李四','王五']
}
print(dic1['fond'])
錯誤提示:KeyError: 'fond'
中譯:key 鍵錯誤:沒有指定的鍵值“fond”
解決辦法:可以給這個字典添加鍵值
dic1['fond'] = '學習'
print(dic1)
9.
dic1 = {
'name':'張三',
'age':17,
'friend':['李四','王五']
}
dic1.pop()
print(dic1)
TypeError: pop expected at least 1 arguments, got 0
類型錯誤:pop方法希望得到至少一個參數,但是現在參數爲0解決辦法:pop裏面需要寫參數,參數爲想要刪除的key值
dic1.pop('friend')
print(dic1)