How to replace a variable value inside a function in python 3

I am new to python and I most likely complicate this. I am having a problem with values ​​in variables inside a function that is not being replaced. Instead, it simply adds a new meaning to it. Using this function, if you select 1 or 2, it is supposed to perform certain actions, if you choose something else, it is assumed that this is not an option, and you will try again. Here is the conclusion if I choose the wrong option before choosing 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 with 1 and clearing the previous values, it seems to just be added 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()

-, , . !

+4
1

, ( testfunc() testfunc()).

, , ( print), .

, , , , , , , .

, :

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()
+3

Source: https://habr.com/ru/post/1568579/


All Articles