我打算获取长度字符串并让程序计数并显示在输入的字符串中找到的字母总数"T"
,但得到以下错误。第13行是这样的:if string[0,counter] == "T":
字符串索引必须是整数 - 子字符串
有什么建议吗?
File "python", line 13, in TypeError: string indices must be integers
#Variable to hold number of Ts in a string
numTs = 0
#Get a sentence from the user.
string = input("Enter a string: ")
#Count the number of Ts in the string.
for counter in range(0,len(string)):
if string[0,counter] == "T":
numTs = numTs + 1
#Display the number of Ts.
print("That string contains {} instances of the letter T.".format(numTs))
感谢,使用索引计数器做到了。 – Cornel