1
def test():
    boo = True
    enter = int(raw_input("Enter something: "))
    if enter > 0:
        boo = False
        print "Did it work?!"
    else:
        print "Still working..."
    while boo == True:
        test()

test()

So this bit of code seems to only work if the first part of the if statement is met first try. aka if you enter -1 then 1 it continues to loop through the function. What am I doing wrong?

4
  • 2
    What are you trying to do exactly? Get a positive integer input from the user? Commented Jul 21, 2014 at 15:49
  • Is recursion intended? Commented Jul 21, 2014 at 15:52
  • 1
    Did you realize that your function is being called in a loop and recursively? That seems a bit odd. Commented Jul 21, 2014 at 15:53
  • It's a bit odd, but it makes for a decent chuckle, anyway :) Commented Jul 21, 2014 at 16:00

2 Answers 2

2

boo is a local variable. Every time you recursively call test(), that recursive call gets a new local variable boo that is independent of all parent functions calling it. As such, it is set to True at the beginning of the function.

Your calls look like this:

  • test(), boo = True, enter = 1, so while boo == True is true:

    • test(), boo = True, enter = -1, so while boo == True is false, returning
  • at this level, boo == True is still true, so we call test() again.

    • test(), boo = True, enter = -1, so while boo == True is false, returning

etc. ad nauseum.

You want to return boo:

def test():
    boo = True
    enter = int(raw_input("Enter something: "))
    if enter > 0:
        boo = False
        print "Did it work?!"
    else:
        print "Still working..."
    while boo:
        boo = test()
    return boo

Now when test() returns, you set boo in the calling function to False.

Note that == True is redundant, just test for boo itself in while boo.

It is really not a good idea to use recursion for user input. Just use a while loop:

while True:
    enter = int(raw_input("Enter something: "))
    if enter < 0:
        print "Did it work?!"
        break
    print "Still working..."
Sign up to request clarification or add additional context in comments.

4 Comments

(+1) but I think it would be great to show that recursion is completely unnecessary here and only serves to obscure the logic.
@NPE: I was busy adding a better option. I wanted to explain about where the OP had gone wrong first.
This is a great explanation. Thanks. I am new to python and will definitely be reading up on recursion. I think putting everything into a while loop will help a lot.
@MartijnPieters: Thanks for expanding the answer. I think it is great.
2

Combining a potentially infinite while loop with a recursive function is a very dangerous game. When you get to subsequent calls of test(), even if they work and exit out right away, your original call to test() will still loop forever. boo is never changed in the original function call just because it's changed in later ones.

Your code could be fixed this way:

def test():
    boo = True
    enter = int(raw_input("Enter something: "))
    if enter > 0:
        boo = False
        print "Did it work?!"
    else:
        print "Still working..."
    if boo == True:
        test()

But it would be even better this way:

def test():
    enter = int(raw_input("Enter something: "))
    while enter <= 0:
        print "Still working..."
        enter = int(raw_input("Enter something: "))
    print "Did it work?!"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.