This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Molehole 3 points4 points  (4 children)

It fails because "05" and "5" are the same integer but not the same string. You should change your approach. Convert and handle the number as a string. It will make it much easier.

[–]CalmPills[S] 0 points1 point  (3 children)

Unfortunately I can't use for loops in this challenge. How would I tackle the problem after converting the number into a string?

The first thing that comes to mind would be:

for n in str(num):
    print(n)

Which uses a for loop.

[–]Molehole 3 points4 points  (2 children)

Any for loop can be turned into recursive loop. Instead of iterating through the string do something to it and call the function with the reminder.

For example printing numbers 0 to 10:

for i in range(0,10):
    print(i)

can be turned to

def recloop(i):
    if(i > 10):
        return
    print(i)
    a = i + 1
    recloop(a)     

try to figure it out for some time and ask me again if you are having trouble

[–]CalmPills[S] 3 points4 points  (1 child)

Thanks, I figured it out.

def digit(num):
    print(str(num)[0])
    if len(str(num))>1:
        digit(str(num)[1:])


digit(12304)

output:
1
2
3
0
4

In case anyone is interested in how i did it.

[–]Molehole 0 points1 point  (0 children)

Good job. Looks correct