元音计数程序问题

问题描述 投票:0回答:1

我正在制作一个小程序来计算给定单词中的元音数量,它适用于没有重复元音的单词,但是当单词太大或重复元音时,数字就不正确。我不太确定出了什么问题,但是一些帮助将不胜感激

vowels=["a","e","i","o","u"
        "A","E","I","O","U"
        ]

def getword():

    word=input("what is the word you aim to count? ")
    length=len(word)
    return length,word

def calcvowels(length,word):

    vowelcount=0
    for i in range(length):
        if word[i] in vowels:
            vowelcount=vowelcount+1
    return vowelcount

def printOutput(vowelcount):

    print(f"There are {vowelcount} vowels in your word")

def main():
    
    leng,word=getword()
    vcount=calcvowels(leng,word)
    printOutput(vcount)


main()

发生了什么:

您要数的单词是什么?花生

你的单词中有2个元音

预计: 您要计算的单词是什么?花生

你的单词中有3个元音

我尝试检查每个字母是否在元音数组中,我希望它能工作,但它导致了上述问题

python arrays debugging
1个回答
0
投票

您缺少一个逗号,这会导致元音计数出现问题。

按如下方式使用它

vowels = ["a","e","i","o","u",
          "A","E","I","O","U"
          ]
© www.soinside.com 2019 - 2024. All rights reserved.