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"))
stringconsists ofcharacters, notnumbers. How exactly are you converting astringinto acollection of numbers?helper_input[0]to int. Try puttingint(helper_input[0]) % 2 == 0as the condition for theifstatement (and its negation for theelifcondition).count_even_odd_recursivereturns nothing, soprintingits result (your last statement) should always printNone.