So my problem is this i have an number like 123 and as the tilte sugests i want the result to be 13.
The problem is that firstly with the method im using im going to get the invert result (31 for example), secondly im getting a zero at the end that shouldn't be there and instead of joining the digits its summing them and i dont understand why. BTW i cant use strings
So to clarify:
My output:
>>> apenas_digitos_impares(123)
40
Correct output:
>>> apenas_digitos_impares(123)
13
program:
def apenas_digitos_impares(n):
if n == 0:
return 0
elif (n%10)%2 == 0:
return apenas_digitos_impares(n//10)
elif (n%10)%2 == 1:
return 10*(n%10) + apenas_digitos_impares(n//10)