1

I simply cannot understand what is happening here. The problem is important for my homework (studying programming so I'm a beginner... also my English is not that good, sorry).

I am trying to read a string... it can be either a number or a set number of commands.
I'll just give a very small example of what I'm trying to do and what is going wrong.

    def validate():
        choice = str(input(">>> "))
        if (choice == "exit"):
            return 0 # should exit validate
        else:
            try:
                aux = int(choice) # Tries converting to integer
            except:
                print("Insert integer or exit")
                validate() # If it can't convert, prompts me to try again through
                           # recursivity
            else:
                return aux
    rezult = validate()
    print (rezult)

Problem is that this small script returns totally random stuff.

If "exit", returns "None".
If first input is correct, it returns correct number.
If first input is an "error" and second input is correct, it's "None" again and I simply can't understand what is going wrong... Why it doesn't want to work or what should I do (alternatively).

2
  • 1
    I really don't think you want to make that a recursive call...why not loop instead? Also, does input() strip newlines/whitespace? That could be an issue if it doesn't, I don't know off the top of my head. Commented Nov 19, 2011 at 22:25
  • Is this Python 2.x or Python 3.x? Commented Nov 19, 2011 at 22:28

3 Answers 3

6

In case you enter the except block, the function validate() uses a recursive call to call itself. When this call returns, it returns to the place where the function was called, i.e. into the except block. The return value of validate() is ignored at this point, and control reaches the end of the outer call without hitting a return statement, so None is implicitly returned.

Don't use recursion here. Use a loop.

Sign up to request clarification or add additional context in comments.

Comments

3

Use raw_input instead of input (unless you are on Python 3.x):

choice = raw_input(">>> ")

And you are missing a return here:

        except:
            print ("Insert integer or exit")
            return validate () # <<< here

Also, don't use recursion for this. Use a loop instead.

Comments

1

Ok, decided to listen and changed the recursive part into a loop, thank you for your help. (Works now)

     def validateChoice():
         condition = False
         while (condition == False):
             choice = str (input (">>> "))
             if (choice == "exit"):
                 return 0
             else:
                 try:
                     aux = int (choice)
                 except:
                     print ("Insert integer or 'exit'")
                 else:
                     condition = True
        return aux

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.