【发布时间】:2017-07-16 20:25:32
【问题描述】:
我想获取一个长度字符串并让程序计数并显示在输入的字符串中找到的字母 "T" 的总数,但出现以下错误。第 13 行是这样的:if string[0,counter] == "T":
有什么建议吗?
文件“python”,第 13 行,在 TypeError: 字符串索引必须是整数
#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))
【问题讨论】: