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
You could do it as follows -
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:
# Include the digit and recurse for remaining...
return (n%10) + 10*apenas_digitos_impares(n//10)
print(apenas_digitos_impares(123))
OUTPUT :
13
The only change that your code needed was in the last line of the function.
You will just include the odd digit(done by n%10
) and,
move on(or recurse) to check for remaining digits. You need to multiply next digit by 10, so - 10*apenas_digitos_impares(n//10)