什麼是 python 中的 TypeError?
TypeError 是 python 編程語言中的異常之一。當對不支持的對象類型執行操作時,或者可以說不是有效的對象類型時,會出現此異常。
- 每當出現異常時都會引發或發生。
- 此外,顧名思義,只要有錯誤的對象,就會出現這種類型的錯誤,我們會對其執行不相關的操作。
- Python 編程語言中的錯誤主要有三種類型:語法錯誤、邏輯錯誤、和異常。
- 這種類型的錯誤屬於異常錯誤的範疇。
- 用戶可以使用 raise 關鍵字輕鬆處理
TypeError
。
TypeError
:字元串索引必須是整數
- 眾所周知,字元串的基本結構由索引值組成。
- 字元串的起始值從索引值 0 開始,它將持續到給定字元串的長度。
- 每當我們想要取出具有指定索引值的字元串的特定字元時,就會出現
TypeError
。不過,我們將輸入不同對象類型的另一個值,而不是在索引中輸入整數值。 - 例如,如果我們想從字元串中獲取字元 2,我們將索引作為整數而不是整數輸入,這將引發
TypeError
字元串索引必須是整數。
讓我們藉助例子來理解它:
TypeError
示例:字元串索引必須是整數
示例 1:給定一個名為「JavaTpoint」的字元串,您需要從中取出特定的字元「T」。T3】
(這裡,我們將討論上述示例中出現TypeError
的所有情況及其解決方案。)
情況 1:我們以字元串格式傳遞數字,而不是在索引中傳遞整數值。
# Python program to fetch the particular character from the string
print("Enter a string: ") # Enter string JavaTpoint
s=input()
print("To fetch out the character 'T' from the string")
s["4"] # As the string begans with index value 0,
# after onwards it will go further by adding 1 to it.
解說:
在上面的例子中,如果我們觀察到這一點,它會產生一個TypeError
:字元串索引必須是整數,因為要從字母表 JavaTpoint 中取出字元「T」,我們必須以整數形式而不是字元串形式傳遞索引值 4。
這裡,我們傳遞值 4,但不是以整數的形式,而是以字元串的形式,正如我們在雙引號中賦予它的那樣。
情況 1 的輸出:
藉助下面提到的更正,讓我們詳細了解一下:
修正案例 1:
# Python program to fetch the particular character from the string
print("Enter a string: ") # Enter string JavaTpoint
s=input()
print("To fetch out the character 'T' from the string")
s[4] # As the string begins with index value 0,
# after onwards it will go further by adding 1 to it.
修正情況 1 的輸出:
情況 2:我們傳遞索引的字元串值,而不是整數值。
# Python program to fetch the particular character from the string
print("Enter a string: ") # Enter string JavaTpoint
s=input()
print("To fetch out the character ' o ' from the string")
s['o'] # As the string begans with index value 0,
# after onwards it will go further by adding 1 to it.
解說:
在上面的例子中,如果我們觀察到這一點,它會產生一個TypeError
:字元串索引必須是整數,因為在這裡,要從字母表 JavaTpoint 中取出字元「o」,我們必須以整數形式而不是字元串形式傳遞索引值 6。
在上面的程序中,我們直接傳遞字母表而不是整數值,而是以字元串的形式傳遞,就像我們在雙引號中賦值一樣。
藉助下面提到的更正,讓我們詳細了解一下:
情況 2 的輸出:
修正案例 2:
# Python program to fetch the particular character from the string
print("Enter a string: ") # Enter string JavaTpoint
s=input()
print("To fetch out the character ' o ' from the string")
s[6] # As the string begins with index value 0,
# after onwards it will go further by adding 1 to it.
修正情況 2 的輸出:
例 2:給定一個名為「奇妙」的字元串,要求你從該字元串中取出特定的切片,或者可以從中說出字元串「der」的一部分,我們稱之為字元串切片。T3】
情況 1:我們以字元串格式傳遞數字,而不是在索引中傳遞整數值。
# Python program to fetch the particular part from the string
print("Enter a string: ") # Enter string Wonderful
s=input()
print("To fetch out the particular slice ' der ' from the string")
s["3":"6"] # As the string begans with index value 0,
# after onwards it will go further by adding 1 to it.
解說:
在上面的例子中,它產生了一個TypeError
:字元串索引必須是一個整數,因為,在這裡,為了從字母奇妙中取出字元串「der」的特定部分,我們必須以整數形式而不是字元串形式傳遞索引值 3: 6。
在上面的程序中,我們需要傳遞值 3: 6,不是以整數的形式,而是以字元串的形式,因為我們已經用雙引號賦予了它。
情況 1 的輸出:
藉助下面提到的更正,讓我們詳細了解一下:
修正案例 1:
# Python program to fetch the particular part from the string
print("Enter a string: ") # Enter string Wonderful
s=input()
print("To fetch out the particular slice ' der ' from the string")
s[3:6] # As the string begans with index value 0,
# after onwards it will go further by adding 1 to it.
修正情況 1 的輸出:
例 3:舉一個字典的例子,我們給出了一個字典集,要求我們從字典中取出特定的關鍵字及其對應的值。T3】
情況 1:當我們在索引中傳遞字元串值,而不是整數值
# Python program to fetch out the particular key with its corresponding value
d1 = { "Shivani" : 19, "Sneha" : 31, "Preeti" : 8, "Abhi" : 50} # declaring a dictionary
for i in d1:
print("Sneha: " + i["Sneha"])
print("Abhi: " + i["Abhi"])
在上面的例子中,它產生了一個TypeError
:字元串索引必須是一個整數,因為這裡要從字典 d1 中取出特定的關鍵字「Sneha」,我們必須傳遞索引 I,它已經是整數形式而不是字元串。
在上面的程序中,我們直接傳遞字元串而不是整數值,而是以字元串的形式傳遞,就像我們在雙引號中賦值一樣。
情況 1 的輸出:
藉助下面提到的更正,讓我們詳細了解一下:
修正案例 1:
# Python program to fetch out the particular key with its corresponding value
d1 = { "Shivani" : 19, "Sneha" : 31, "Preeti" : 8, "Abhi" : 50} # declaring a dictionary
for i in d1:
print(i)
修正情況 1 的輸出:
處理TypeError
:字元串索引必須是整數
在上面的例子中,我們看到當我們以非整數值的錯誤格式輸入字元串時,可能會產生TypeError
。雖然我們已經看到了所有例子的修正,但是我們必須知道如何處理所有這樣的異常。
為了在 python 編程語言中處理這些異常,我們通常會使用「try」和「except」關鍵字 try block。我們將輸入所有的測試用例,主要是儘可能多,其中這是產生TypeError
的可能性。我們將通過列印用戶定義的錯誤消息,在 except block 中輸入處理上述異常的方式。
讓我們藉助一個例子更詳細地了解
例:
# Python program to handle the type error exception
l1 = ["Apple", "Mango", "Banana", "Grapes"] # declaring a list
indices = [0, 1, 2, "2", 3]
for i in range(len(indices)):
try:
print(l1[indices[i]]) # case which generates TypeError
except TypeError:
print("TypeError: Check list of indices")
以上異常處理示例的輸出:
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/219597.html