print("<<== program to select and display words of an user input string ==>>")
s=str(input("enter a string: "))
split1=s.split()
len1=len(split1)
header1="----------------------------------------------"
heading="printing words and ther indexes"
print(header1)
print(heading.center(45))
print(header1)
for x in range(0,len1):
print(" ",x,"---->",split1[x])
print(header1)
print('')
print('')
g=input('enter the index of the word to display: ')
print(split1[g])
我正進入(狀態:
line 16, in <module>
print(split1[g])
TypeError: list indices must be integers or slices, not str
uj5u.com熱心網友回復:
input()string默認回傳。您必須使用int()將其轉換為整數,因為索引不能是字串。所以,它需要是這樣的:
g = int(input('enter the index of the word to display: '))
print(split1[g])
編輯:我知道你想用變數做什么g
為此,你必須像這樣使用 index()
g = input('enter the index of the word to display: ')
print(split1.index(g))
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/311509.html
上一篇:在串列中附加串列有一些問題
下一篇:java.lang.NumberFormatException:對于輸入字串:“id”,同時嘗試將相關表列檢索到jsp選擇框
