title: update a variable within a function?
body: <p>I'm teaching myself python 3.x and am struggling to resolve an issue I'm having with variable scope. </p>
  
  <p>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'.</p>
  
  <p>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.</p>
  
  <p>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'.</p>
  
  <p>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?</p>
  
  <pre><code>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)
  </code></pre>