update a variable within a function?
Question: I'm teaching myself python 3.x and am struggling to resolve an issue I'm
having with variable scope.

In my program, I ask the user for an input, and then run a function to check
its value. If the value is correct, I continue. Otherwise, I wish to allow the
user to change their previous input and retest with the new value. The values
I am looking for are the strings: '0', '1', '2',... '8', '9'.

The code below is incorrect, but its the best I've been able to come up with
so far. The problem I see is that while I can change the variable user_input
within the function check_input(), I do not know how to change the scope of
the variable.

For example, if I run the program and type 'abc' for rating_A, I will get the
'too many characters' message, and will be asked to try again. I now enter
'7', and all seems dandy. But when I call rating_A later, I will get 'abc',
not the desired '7'.

I've tried to fix this with the global variable technique. My difficulty with
that is that I wish to check a large amount of input values, and each will
have its own variable (rating_A, rating_B.... rating_Y, rating_Z). I imagine
there is a much more elegant way to accomplish this?

    
    
    from string import digits
    
    def check_input(user_input):
    
        while True:
    
            if len(user_input) != 1:
                print (user_input, 'has too many characters!')
                user_input = input('try again: ')
                continue
    
            elif user_input in digits:
                print (user_input, 'is valid!')
                user_input = int(user_input)
                return user_input
                break
    
            else:
                print (user_input, 'is not a valid input!')
                user_input = input('try again: ')
                continue
    
    
    rating_A = input('Please enter a value for A: ')
    check_input(rating_A)
    print ()
    rating_B = input('Please enter a value for B: ')
    check_input(rating_B)
    


Answer: Not sure I understand you really, but in case it's valid, you are already
returning user_input, thus

    
    
    rating_A = input('Please enter a value for A: ')
    rating_A = check_input(rating_A)
    

assigns the final return value to your variable and you are set.

Otherwise I apologize for not understanding your request well.

