gpt4 book ai didi

python 'String Index Out of Range Error' ?

转载 作者:行者123 更新时间:2023-11-28 21:57:14 25 4
gpt4 key购买 nike

我今年 15 岁,目前正在攻读计算机专业的 GCSE。我的知识非常基础,我在为“元音值(value)计算器”编写的一段代码中遇到了一些麻烦,该代码应该检查一个单词并根据数量和数量给它一个元音分数它有元音。我不断收到错误消息,我完全被难住了,我们将不胜感激。这是我的源代码:

元音值计数器

print('Welcome to the Vowel Worth Counter!')

word = input('Please input your word, in lower-case, or type Q to quit.')

if word == 'Q' :
quit()

def vowelcount(word) :
lettercount = int(len(word))
vowelscore = 0
checkcount = 1
position = 0
while lettercount != checkcount :
if word[position] == str('a') :
vowelscore = vowelscore + 5
if word[position] == str('e') :
vowelscore = vowelscore + 4
if word[position] == str('i') :
vowelscore = vowelscore + 5
if word[position] == str('o') :
vowelscore = vowelscore + 5
if word[position] == str('u') :
vowelscore = vowelscore + 5
position = position + 1
if lettercount == checkcount :
print('I have finished calculatiing your Vowel Score.')
print('Your Vowel score is ' + str(vowelscore) + '!')
for x in range (0,1) :
break
vowelcount(word)

如我所说,我们将不胜感激,谢谢。

最佳答案

循环中的退出条件应该是:

while position < lettercount:

或者更简单,您可以像这样遍历字符串中的每个字符:

for c in word:
if c == 'a':
# and so on

关于 python 'String Index Out of Range Error' ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20107898/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com