Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x Python:递归(没有循环或列表,它必须严格保持为字符串)一个字符串,以求元音/辅音(大写和小写)之间的净分数之和_Python 3.x_Recursion - Fatal编程技术网

Python 3.x Python:递归(没有循环或列表,它必须严格保持为字符串)一个字符串,以求元音/辅音(大写和小写)之间的净分数之和

Python 3.x Python:递归(没有循环或列表,它必须严格保持为字符串)一个字符串,以求元音/辅音(大写和小写)之间的净分数之和,python-3.x,recursion,Python 3.x,Recursion,更详细地 小写元音(aeiou)值为+1 小写辅音(即英语字母表中的剩余字母)值为-1 大写元音为+2,而大写辅音为-2 我的代码到目前为止我不知道哪里出了问题,因为我不断地出错 def count_vowels(s): lower_vowels = 'aeiou' upper_vowels = 'AEIOU' if s[0] == '': return 0 elif s[0] in lower_vowels: return 1 + c

更详细地

  • 小写元音(aeiou)值为+1
  • 小写辅音(即英语字母表中的剩余字母)值为-1
  • 大写元音为+2,而大写辅音为-2
  • 我的代码到目前为止我不知道哪里出了问题,因为我不断地出错

    def count_vowels(s):
        lower_vowels = 'aeiou'
        upper_vowels = 'AEIOU'
        if s[0] == '':
           return 0
        elif s[0] in lower_vowels:
           return 1 + count_vowels(s[1:])
        elif s[0] in upper_vowels:
           return 2 + count_vowels(s[1:])
        elif s[0] not in lower_vowels:
           return -1 + count_vowels(s[1:])
        elif s[0] not in upper_vowels:
           return -2 + count_vowels(s[1:])
        else:
           return count_vowels(s[1:])
    
    任何帮助都将不胜感激,谢谢

    这个怎么样

    def count_vowels(s):
        lower_vowels = 'aeiou'
        upper_vowels = 'AEIOU'
        if len(s) == 0:
            return 0
        if s[0] in lower_vowels:
           return 1 + count_vowels(s[1:])
        elif s[0] in upper_vowels:
           return 2 + count_vowels(s[1:])
        elif s[0].isupper()
           return -2 + count_vowels(s[1:])
        else:
           return -1 + count_vowels(s[1:])   
    print count_vowels('efghjgbfvjhvbj')
    
    from string import ascii_letters # all letters
    vowel="aeiouAEIOU"
    
    def net_score(text):
        if text: # the same as len(text)!=0
            c = text[0]   
            if c in ascii_letters: # this is to help ignore non-letters like spaces, ".,!" etc
                s = 1 if c.islower() else 2 # the value of the letter depending on if is upper or lower
                if c in vowel:
                    return s + net_score(text[1:])
                else:
                    return -s + net_score(text[1:])
            else: # in case of a non-letter continue with the next
                return net_score(text[1:])
        else: #base case
            return 0
    
    快速测试

    >>> net_score("Hello World!")
    -6
    >>> 
    

    第四分支永远不会被执行。想想看。无论如何,基本大小写应该是空字符串。当函数接收到空字符串作为参数时,它应该返回
    0
    。否则它将抛出
    indexer
    @MarcosModenesi-啊,我明白你的意思了,我将else语句更改为另一个elif,其中s[0]不能位于上\u元音中,而else语句只返回来自s[1:]的递归调用然而,当我尝试像“dog”这样的简单测试时,我仍然收到一个错误,可能你错过了字符甚至不是元音的情况。记住,当下元音中的
    s[0]为
    False
    时,下元音中的
    s[0]的布尔值是多少。请阅读并遵循帮助文档中的发布指南。适用于这里。在您发布MCVE代码并准确描述问题之前,我们无法有效地帮助您。“我不断出错”不是一个问题说明。第四个分支永远不会执行。这是问题的一部分,也是我排除异常的一部分,但除此之外,正如Marcos所说,算法也不正确。@trixn您可以指定错误在哪里或我如何修复它吗?您的一些条件不正确。我相信你会发现问题的。想一想:当下元音中的
    s[0]为
    False
    时,
    s[0]不在下元音中的布尔值是什么?
    elif s[0]不在下元音中:
    codeflow只有在s[0]不是元音(上元音或下元音)时才会到达这个elif。对于所有其他字母(辅音大写或小写)将贡献-1,这是错误的