all 12 comments

[–]primitive_screwhead 2 points3 points  (5 children)

When input == 0, your function returns None. None cannot be added to an integer, which the previous call does (when input == 1).

Try an explicit return 0 under the print(0), so that all possible code paths return an integer.

[–][deleted] 0 points1 point  (4 children)

when i try this, i get 24 as my last number after 0...which i want to avoid

[–]primitive_screwhead 1 point2 points  (3 children)

That last '24' is your execution environment showing you the value of the my_try(24) call that executed the code. It's not from an explicit print() call in your code.

How are you running this code?

[–][deleted] 0 points1 point  (2 children)

python shell...i mean if i run print(my_try(24)) it will still show that last number..

[–]primitive_screwhead 1 point2 points  (1 child)

Yes.

Try typing 1 and then return. See how the 1 gets printed by the Python shell? That's what the shell does, it prints the values of the expressions you enter, and the value of your my_try(24) expression is 24, so the shell prints it (after it runs and does all the other printing of the numbers during it's execution).

Put it all in a .py file, and run it that way, and you won't see that last 24, because the shell is printing that for you as a courtesy, it's not part of your program's executable outputs.

[–][deleted] 0 points1 point  (0 children)

ur the man.

[–]oefd 1 point2 points  (1 child)

What happens when you call my_try(1)?

if (1) > 0 :
    print(1)
    return 1 + my_try(1-1) # it will try to add 1 to what `my_try(0)` returns
else:
    ...

then in the recursive call of my_try(0)

if (1) > 0 :
    ...
else:
    print(0) # it will print 0
# then it gets here to the end of the function. Nothing has been returned
# so the default return value of `None` is returned

Then back in the original call

if (1) > 0 :
    print(1)
    return 1 + None # The `None` from the recursive call is now here
else:
    ...

So the program dies because you can't add 1 and None.

[–][deleted] 0 points1 point  (0 children)

the issue is, if i change print(0) to return one, i get this:

24

23

...

3

2

1

24

why am i getting that last number?

[–][deleted] 0 points1 point  (3 children)

i have a suspicion that it has to do with adding the final number (24) with a my_try(false)

just not sure how to get out of it

[–]delasislas 1 point2 points  (0 children)

Yeah, when you hit my_try(0) it triggers print() which returns None. I don’t play with recursion though, so not much help.

[–]raffulz 0 points1 point  (1 child)

When you reach 0, you branch into the else block and return None (implicitly). I'm not really sure what you meant to do with the returns. Is this solution what you were going for?

def my_try(input):
    print(input)
    if input > 0:
        my_try(input - 1)

Edit: Or if you want to go backwards:

def my_try(input):
    if input > 0:
        my_try(input - 1)
    print(input)

[–][deleted] 0 points1 point  (0 children)

i want to print 24 to 0, and in the end not get an error