嗨,我只编写了很短的一段时间,正在尝试编写一个程序,计算一个句子中的元音,然后在我得到错误的那一刻将该数字返回给用户:
TypeError:'内置函数\或\方法'对象不可下标'
vowel = "aeiou"
vcount = 0
index = 0
sen = input("Enter sentence (If you enter nothing the program will terminate)\n")
senLen = len(sen)
while senLen > 0:
sen = sen.lower
while index < senLen:
if "test"[index] in vowel:
vcount = vcount + 1
index = index + 1
print("You latest sentance was", sen, "\nIt was", senLen, "Characters long\n", "And had", vcount, "Vowels")
input("Enter sentence (If you enter nothing the program will terminate)\n")
提前谢谢
在这里:
您没有将
sen
设置为原始字符串的小写版本,而是设置为完成此操作的string方法。要实际运行该方法并返回小写字符串,需要使用()
调用它:那么
sen[index]
将和你的实验"test"[index]
一样有效。你知道吗但是,您的程序仍然无法正常工作。一旦内部循环到达一个给定字符不是元音的点,它就会停止增加索引,因此它会永远检查同一个非元音字符。取消那条线。此外,您保存第一句话的长度,然后从不更新它。相反,每次用户输入一个句子时都要计算它。另外,您没有在循环中保存新的
sen
。最后,您永远不会重置index
和vcount
,因此该方面也不会处理新句子。你知道吗通过使用
break
语句、sum()
函数和多个print()
调用,可以使代码更简短、更易于阅读:相关问题 更多 >
编程相关推荐