0
def check(x):
   global username
   while True: 
       if x.isalpha(): 
          break 
       else:
            x = input("Please type your name with letters only!: ")
            continue

username = input("? ")
check(username)
print (username)

I have a problem with this code, if the first user input is not alpha, the program will ask for the user's input again until the user inputs the right input by using only alphabetical letters. But when I print the value inside the (username) variable after that, I will get the first user's input even though it is the wrong input and he has already changed it inside the function check():. I have tried to use many solutions, but it did not work. I think it is a problem related to the global variables although I have set the (username) variable as a global variable. If anyone has got any solution for this problem, please help me.

Y.Hasan
  • 11
  • 7
  • 2
    do `username = x` at the end of the function – tdelaney Jan 16 '16 at 00:23
  • Thank you for your solution and quick reply. What if I want to use the function for other variables not only for the (username) variable. For example I want to apply the same function on a (SureName) variable. – Y.Hasan Jan 16 '16 at 00:29
  • Possible duplicate of [How do I pass a variable by reference?](http://stackoverflow.com/questions/986006/how-do-i-pass-a-variable-by-reference) – Barmar Jan 16 '16 at 00:46

2 Answers2

0

Yes because once you're inside the loop you're setting the variable x to the input... but x is never revisited. Revised:

def check():
   global username
   while True: 
       if username.isalpha(): 
          break 
       else:
            username = input("Please type your name with letters only!: ")
username = input("? ")
check()
print (username)

Also a somewhat less convoluted example, no need for globals here:

def get_username():
    username = input("? ")
    while not username.isalpha():
        username = input("Please type your name with letters only!: ")
    return username
print (get_username())

And unless this is specifically for practice with globals, I would avoid them at all costs. You should always use the minimum necessary scope for your variables, it's good hygiene.

More generalized input function in response to your comment:

def get_input(inputName):
    '''Takes the name of the input value and requests it from the user, only
        allowing alpha characters.'''
    user_input = input("Please enter your {} ".format(inputName))
    while not user_input.isalpha():
        user_input = input("Please type your {} with letters only!: ".
            format(inputName))
    return user_input
username = get_input("Username")
surname = get_input("Last Name")
print(username, surname)
ktbiz
  • 586
  • 4
  • 13
  • Thank you for your solution and quick reply. What if I want to use the function for other variables not only for the (username) variable. For example I want to apply the same function on a (SureName) variable. – Y.Hasan Jan 16 '16 at 00:31
  • Thank you. The last solution is the solution that I was looking for. I just need to understand how does it work. – Y.Hasan Jan 16 '16 at 00:46
  • `get_input()` accepts the name of the thing you want, eg "username" or "last name" and then asks the user for the input. While the input is not all alpha, it keeps asking until it gets an input that is all alpha. Then it returns the value that was input. No need to utilize global variables here. Also I changed input() to raw_input() which is what you want here. – ktbiz Jan 16 '16 at 00:52
  • Thank you very much, that was helpful. – Y.Hasan Jan 16 '16 at 00:55
  • I have got a problem with the raw_input(), it is not working with me in python 3. Every time it gives me this error: Traceback (most recent call last): File "C:\Users\khalqadhi\Documents\yazeed\prac.py", line 6, in username = raw_input("Please type your name: ") NameError: name 'raw_input' is not defined – Y.Hasan Jan 16 '16 at 20:40
  • `raw_input()` was eliminated in 3.x so use `input()`. Sorry for the confusion I didn't see the 3.x tag. – ktbiz Jan 16 '16 at 22:20
0

The issue is you were passing "username" by value but treating it like you were passing it by reference.

This is my code. :) I hope it helps. (I wrote this using Python 2.7 but it should work with Python 3)

def check(x):
    while x.isalpha() is not True: 
        x = raw_input("Please type your name with letters only!: ")
    return x

username = raw_input("Please type your name: ")
username = check(username)
print ("Your name is: " + username)

if you absolutely NEED to use a global variable you do not need to pass it to the function.

def check():
    global username
    while username.isalpha() is not True: 
        username = raw_input("Please type your name with letters only!: ")

username = raw_input("Please type your name: ")
check()
print ("Your name is: " + username)
Dustin Nunyo
  • 96
  • 1
  • 9
  • Thank you your code is working. But I had to change change raw_input to input. I don't know why python 3 didn't recognize it. – Y.Hasan Jan 16 '16 at 20:29