嗨我只编写了一段时间,我正在尝试编写一个程序来计算一个句子中的元音,然后在我收到错误时将该数字返回给用户:
TypeError:'builtin_function_or_method'对象不可订阅'
<div>
<select name="test" id="test">
<option>Lorem</option>
<option>Ipsum</option>
<option>Dolor</option>
<option>Sit</option>
<option>Amet</option>
</select>
<span>Open select</span>
</div>
提前致谢
答案 0 :(得分:0)
在这里:
sen = sen.lower
您将sen
设置为不是原始字符串的小写版本,而是设置为完成此操作的字符串方法。要实际运行该方法并返回小写字符串,您需要使用()
:
sen = sen.lower()
然后sen[index]
将与您的实验"test"[index]
一样有效。
但是,您的程序仍然无法正常运行。一旦内循环到达给定字符不是元音的点,它就会停止增加索引,因此它会一直检查同一个非元音字符。不知道那条线。此外,您保存第一个句子的长度,然后永远不会更新它。相反,每次用户输入句子时计算它。此外,您没有在循环中保存新的sen
。最后,您永远不会重置index
和vcount
,因此该方面也无法处理新句子。
vowel = "aeiou"
sen = input("Enter sentence (If you enter nothing the program will terminate)\n")
senLen = len(sen)
while senLen > 0:
index = 0
vcount = 0
sen = sen.lower()
while index < senLen:
if sen[index] in vowel:
vcount = vcount + 1
index = index + 1
print("Your latest sentence was", sen, "\nIt was", senLen, "Characters long\n", "And had", vcount, "Vowels")
sen = input("Enter sentence (If you enter nothing the program will terminate)\n")
senLen = len(sen)
您可以使用break
语句,sum()
函数和多个print()
来电,让您的代码更简短易读:
vowel = "aeiou"
while 1:
sen = input("Enter sentence (If you enter nothing the program will terminate)\n").lower()
if not sen:
break
vcount = sum(c in vowel for c in sen)
print("Your latest sentence was", sen)
print("It was", len(sen), "characters long")
print("And had", vcount, "vowels")