【问题标题】:Python Count Odd and Even in String of Numbers Using RecursionPython使用递归计算数字字符串中的奇数和偶数
【发布时间】:2021-09-14 17:08:30
【问题描述】:

我正在尝试在我的 Python 程序中使用 RECURSION 计算一串数字中的所有偶数和奇数,但它一直向我显示此错误:“TypeError:并非所有参数在字符串格式化期间都转换...”请,可以有人帮帮我吗?

我在 JS 中尝试过,效果很好……但在 python 上却不行。我觉得我做错了什么。

下面是我的代码:

def count_even_odd_recursive(string):
    def helper(helper_input):
        odd = 0
        even = 0
        if len(helper_input) == 0:
            return
        if helper_input[0] % 2 == 0:
            even += 1

        elif helper_input[0] % 2 != 0:
            odd += 1

        helper(helper_input[1::])

        if even > odd:
            return f'There are more even numbers ({even}) that odd.'
        else:
            return f'There are more odd numbers ({odd}) that even.'

    helper(string)
print(count_even_odd_recursive("0987650"))

【问题讨论】:

  • 通常,stringcharacters 组成,而不是 numbers。您究竟是如何将 string 转换为 collection of numbers 的?
  • 您需要将helper_input[0] 转换为int。尝试将int(helper_input[0]) % 2 == 0 作为if 语句的条件(以及对elif 条件的否定)。
  • 最重要的是,count_even_odd_recursive 不返回任何内容,因此printing 其结果(您的最后一条语句)应始终打印None
  • @zr0gravity7 我将它转换为 int,它给了我 None 作为输出。
  • 返回助手(字符串)

标签: python python-3.x recursion


【解决方案1】:

有几个问题,第一个是当您检查数字是否为偶数时,您正在尝试对字符串执行模运算符。您可以通过使用int() 进行强制转换来轻松解决此问题。

其次,您的变量 evenodd 是本地变量,您实际上并没有将它们传递给对 helper 的递归调用。老实说,我不确定为什么这需要递归,但不幸的是在您提供的代码中没有发生递归。如果您希望它是递归的,我已经对其进行了一些修改,还添加了偶数与奇数相同的情况:

def helper(helper_input):
    
    if len(helper_input) == 0:
        return 0
    
    if int(helper_input[0]) % 2 == 0:
        return helper(helper_input[1:]) + 1

    return helper(helper_input[1:]) + 0

def count_even_odd_recursive(string):
    even = 0
    if len(string) == 0:
        return "No input provided"
    else:
        even = helper(string)
        
        if even > len(string) / 2:
            return f'There are more even numbers ({even}) than odd.'
        elif even == len(string) / 2:
            return f'There are more odd numbers ({len(string)-even}) than even.'
        else:
            return "Equal digits odd and even."
    
print(count_even_odd_recursive("0987650"))

【讨论】:

    【解决方案2】:

    这应该可以解决您的错误:

    def count_even_odd_recursive(string):
        def helper(helper_input):
            if len(helper_input) == 0:
                return 0, 0
                
            odd, even = helper(helper_input[1:])
    
            if int(helper_input[0]) % 2 == 0:
                even += 1
            else:
                odd += 1  
    
            return odd, even
                
        odd, even = helper(string)
        
        if even > odd:
            return f'There are more even numbers ({even}) that odd.'
        else:
            return f'There are more odd numbers ({odd}) or the same as even numbers.'
    print(count_even_odd_recursive("0987650"))
    
    # There are more even numbers (4) that odd.
    

    我也修复了你的递归,不会撒谎,我不知道你打算如何在那里进行递归,但这是递归的,它有效。

    【讨论】:

    • 非常感谢!该解决方案效果很好!我实际上用 Javascript 解决了它,并在 python 中使用了相同的想法,但没有成功。再次感谢,如果您不介意,我很乐意与您联系。
    • @SamuelAnumudu 如果我的解决方案对您有帮助,如果您接受,我将不胜感激:)
    猜你喜欢
    • 2019-07-04
    • 2014-03-31
    • 1970-01-01
    • 1970-01-01
    • 2011-06-12
    • 2022-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多