Python 需要帮助!计算元音的用户字符串输入

Python 需要帮助!计算元音的用户字符串输入,python,string,function,count,Python,String,Function,Count,这是一项让我难堪的作业。它必须包含一个主要功能 编写一个程序,提示用户输入字符串,然后将字符串发送给函数 调用CountVowells(stringToCheck),它确定字符串中的元音数并返回 价值观 我对你的代码做了一些修改 您正在通过StringToCheck循环,因此它正在查看整个字符串是否是元音,而不仅仅是一个字符 print语句在for循环中,因此它打印出每个循环的元音数 在每个循环中将count设置为0,这会破坏计数点 我对您的代码做了一些更改: count = 0 def

这是一项让我难堪的作业。它必须包含一个主要功能

编写一个程序,提示用户输入字符串,然后将字符串发送给函数 调用CountVowells(stringToCheck),它确定字符串中的元音数并返回 价值观


我对你的代码做了一些修改

  • 您正在通过StringToCheck循环,因此它正在查看整个字符串是否是元音,而不仅仅是一个字符

  • print语句在for循环中,因此它打印出每个循环的元音数

  • 在每个循环中将count设置为0,这会破坏计数点


  • 我对您的代码做了一些更改:

    count = 0
    def main():
        print('This program will calculate the number of vowels in a string of characters.')
        stringInput = input('Enter a string: ')
        countVowels(stringInput)
    
    def countVowels(stringToCheck):
            count = 0
            vowels = ["a", "e", "i", "o", "u"]
            for currentChar in stringToCheck:
                for CurrentElement in vowels:
                    if currentChar == element:
                        count = count + 1
            print('This string contains', count, 'vowels.')
        
    main()
    
    让我们看看我做了什么

    我首先将
    元音
    变量更改为一个列表,因为这样保存多个值更容易、更有效。在函数
    countvowells()
    中,我添加了一个for循环来检查
    currentChar
    是否位于
    vowells
    变量的
    currentElement
    中。如果是这样,它将
    计数增加一

    我测试了这个,它对我有效


    祝你好运

    请将您的代码格式化为代码(``而不是**)。您的问题是什么<代码>当前字符
    未使用。您可能应该使用它。我更改了count=0的位置,就像您在这里所做的那样。我从来没有在这里发布过帖子,并且无意中切断了上面的内容,比如def main():我仍然存在的问题是老师需要一个def main()来完成它。他还希望输出像这样显示“字符串包含3个元音。”而我的结果是“此字符串包含1个元音。”和“此字符串包含2个元音”。例如,当输入为eerr时,我在看到您的编辑后添加了def main。打印两次是因为您最初在for循环中有print语句(我刚刚为您更改了缩进),现在可以工作了。非常感谢你。
    
    def main():
        print('This program will calculate the number of vowels in a string of characters.')
        stringInput = input('Enter a string: ')
        countVowels(stringInput)
    
    
    def countVowels(stringToCheck):
        vowels = "aeiou"
        count = 0
        for currentChar in stringToCheck:
            if currentChar in vowels:
                count = count + 1
        print('This string contains', count, 'vowels.')
    
    main()
    
    
    count = 0
    def main():
        print('This program will calculate the number of vowels in a string of characters.')
        stringInput = input('Enter a string: ')
        countVowels(stringInput)
    
    def countVowels(stringToCheck):
            count = 0
            vowels = ["a", "e", "i", "o", "u"]
            for currentChar in stringToCheck:
                for CurrentElement in vowels:
                    if currentChar == element:
                        count = count + 1
            print('This string contains', count, 'vowels.')
        
    main()