4

I am new to python, and I am likely over complicating this. I am having problem with values in variables within a function not being replaced. Instead, it seems to just be adding the new value to it. With this function, if you select 1 or 2, it is supposed to perform certain actions, if you select anything else, it is supposed to say that is not an option and have you try again. Here is the output if I select a wrong option before selecting a valid option:

Select Option

[1] Option 1
[2] Option 2

You selected Option 1
1
9
7
6

So instead of replacing the value of my variable to 1 and clearing previous values, it seems to just add to it. Here is my code:

def testfunc():

    testvar = input('Select Option\n\n[1] Option 1\n[2] Option 2\n\n    >')
    if testvar in ('1', 'Option 1', 'option 1'):
        print("\nYou selected Option 1")
    elif testvar in ('2', 'Option 2', 'option 2'):
        print("\nYou Selected Option 2")
    else:
        print("\n\nThat is not an option, please select another option")
        testfunc()
    print(testvar)

testfunc()

If I could have someone explain why its storing multiple values and how I can prevent this, I would really appreciate it. Thanks!

10
  • 1
    You are printing the value after you call the function again, so its printing the last value. Put the print function after the testvar = input() line. Commented Dec 22, 2014 at 5:27
  • That was exactly it. Very simple. If you put in a answer I will mark it as the solution. Thank you very much. Commented Dec 22, 2014 at 5:32
  • You can answer your own question :) Commented Dec 22, 2014 at 5:33
  • @Stephen, that is so much not the correct answer :-) There is absolutely zero reason to use recursion here and you should change your structure to be a loop instead. Commented Dec 22, 2014 at 5:34
  • @paxdiablo: it is ok to use recursion here (a thousand invalid attempts before stackoverflow is more than enough for a human). Commented Dec 22, 2014 at 5:36

1 Answer 1

3

Your problem is that you use recursion to go back and get another value (the call to testfunc() within testfunc() itself).

If you enter an invalid value, it will call the function again, saving its current position (before the print) so it can return to there.

Then, when you finally enter a good value, it will eventually unwind the stack, printing out the invalid ones as you go.

You are better off using a loop for this sort of validation, something like:

def testfunc():
    ok = False
    while not ok:
        testvar = input('Select Option\n\n[1] Option 1\n[2] Option 2\n\n    >')
        if testvar in ('1', 'Option 1', 'option 1'):
            print("\nYou selected Option 1")
            ok = True
        elif testvar in ('2', 'Option 2', 'option 2'):
            print("\nYou Selected Option 2")
            ok = True
        else:
            print("\n\nThat is not an option, please select another option")

    print(testvar)

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

1 Comment

read your answer after the initial comment on my question, and I appreciate the explanation as well as the example very much.

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.