0

I am trying to count all the even and odd numbers in a string of numbers using RECURSION in my Python program but it keeps showing me this error: "TypeError: not all arguments converted during string formatting..." Please, can anyone help me out here?

I tried it in JS it worked perfectly...but not on python. I think I am doing something wrong.

BELOW IS MY CODE:

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"))
7
  • Usually, a string consists of characters, not numbers. How exactly are you converting a string into a collection of numbers? Commented Jul 2, 2021 at 19:27
  • You need to cast helper_input[0] to int. Try putting int(helper_input[0]) % 2 == 0 as the condition for the if statement (and its negation for the elif condition). Commented Jul 2, 2021 at 19:28
  • Most importantly, count_even_odd_recursive returns nothing, so printing its result (your last statement) should always print None. Commented Jul 2, 2021 at 19:29
  • @zr0gravity7 I casted it to int, it's gave me None as output. Commented Jul 2, 2021 at 19:30
  • 1
    return helper(string) Commented Jul 2, 2021 at 19:31

2 Answers 2

1

This should fix your error:

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.

I fixed your recursion also, not gonna lie, I had no idea how you intended the recursion to happen there, but this is recursive and it works.

Sign up to request clarification or add additional context in comments.

2 Comments

thank you very much! That solution worked just fine! I actually solved it in Javascript and used the same idea in python but didn't work out. Thanks again, I would love to connect with you if you don't mind.
@SamuelAnumudu If my solution helped you, I would appretiate if you accepted it :)
1

There's a couple of problems, the first is that when you're checking whether the digit is even, you're trying to perform modulo operator on a string. You can fix this easily by casting using int().

Secondly, your variables even and odd are local, and you're not actually passing them into the recursive calls to helper. To be honest, I'm not sure why this needs to be recursive, but unfortunately in the code you provided there is no recursion happening. If you want it to be recursive, I've reworked it a bit, also adding in a case where the the even digits are the same count as the odd digits:

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"))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.