-4

I made a ATM function program, I need help because I can't display my username(un) and password(pw) in my program the error is

NameError: name 'un' is not defined

I tried defining un in the first line, but the value does not change after the user has input their information.

#functions
def FWelcome():
    print("WELCOME!")
    print("Please Log-In to access ATM:")
    return;

def FUsername():
    while True:
        un=input("\nEnter Username ( use only letters ):")
        if un.isalpha():
            break
        else : #invalid
            print ("Invalid Username. Use of symbols and/or numbers are not allowed.\n")
    return;

def FPassword():
    while True:
        pw=input("Enter Password ( use only numbers ):")
        if pw.isnumeric():
            break
        else : #invalid
            print ("Invalid Password. Use of symbols and/or letters are not allowed.\n")
    return;

#atm program
FWelcome()

#username
FUsername()

#password
FPassword()

print("\nHello! Your Username is ", un, " and your password is ",pw )
DavidG
  • 24,279
  • 14
  • 89
  • 82
Haneul Kim
  • 1
  • 1
  • 1
  • 2
  • 1
    You need to `return` the values of `un` and `pw` in order to use them outside of your functions. – DavidG Oct 10 '17 at 13:36

3 Answers3

1

You should return the values:

def FUsername():
    while True:
        un=input("\nEnter Username ( use only letters ):")
        if un.isalpha():
            break
        else : #invalid
            print ("Invalid Username. Use of symbols and/or numbers are not allowed.\n")
    return un

def FPassword():
    while True:
        pw=input("Enter Password ( use only numbers ):")
        if pw.isnumeric():
            break
        else : #invalid
            print ("Invalid Password. Use of symbols and/or letters are not allowed.\n")
    return pw

#atm program
FWelcome()

#username
un = FUsername()

#password
pw = FPassword()
Arthur Gouveia
  • 734
  • 4
  • 12
  • Returning the values is necessary, but not sufficient; the values also need to be assigned to variables when calling the functions. You know this already, of course, because your suggested code does exactly that. I just think it's worth reiterating in prose :-) – Kevin Oct 10 '17 at 13:46
  • Thank you so much, and im sorry for not being able to find the post which is what i am asking – Haneul Kim Oct 10 '17 at 13:47
0

un is defined inside FUsername and it is therefore inaccessible globally. You'll either have to place the "print un" statement inside the function or declare it with a default dummy value globally then update it in the FUsername

Stephen Nyamweya
  • 104
  • 1
  • 11
-1

You declare un inside of a function, but try to access it outside of that function. One way to solve this would be to return the values. Another way is to make the variable global.

#Comment the 2 below lines out if you want to return the variables from the function.
un=input("\nEnter Username (use only letters ):")
pw=input("Enter Password ( use only numbers ):")
def FWelcome():
    print("WELCOME!")
    print("Please Log-In to access ATM:")
    return;

def FUsername():
    while True:
        #Comment this out if you want to declare it globally
        un=input("\nEnter Username ( use only letters ):")
        if un.isalpha():
            break
        else : #invalid
            print ("Invalid Username. Use of symbols and/or numbers are not allowed.\n")
    return un

def FPassword():
    while True:
        #Comment this out if you want to declare it globally.
        pw=input("Enter Password ( use only numbers ):")
        if pw.isnumeric():
            break
        else : #invalid
            print ("Invalid Password. Use of symbols and/or letters are not allowed.\n")
 #Or return the variable un
    return pw

#atm program
FWelcome()

#username
FUsername()

#password
FPassword()



print("\nHello! Your Username is ", un, " and your password is ",pw )
wanderer0810
  • 400
  • 1
  • 7
  • 20
  • FYI, you don't need the semi colon at the end of the return statement – DavidG Oct 10 '17 at 13:45
  • Ok, `un` and `pw` now exist at the global scope, so the NameError won't happen any more; that's good. But now the program asks for user name and password twice each, and it ignores the second value you put in for each one, instead sticking with the first values regardless of whether they are actually valid inputs; that's bad. – Kevin Oct 10 '17 at 13:49
  • You need to comment out the second time it asks for the user input. Sorry, I thought I put in a comment mentioning that, but I forgot. I will edit the post to reflect that. – wanderer0810 Oct 10 '17 at 13:51
  • @DavidG Thanks, I guess that's just habit – wanderer0810 Oct 10 '17 at 13:51
  • Thank you guys, I now learned how to use function properly. Have a nice day – Haneul Kim Oct 10 '17 at 13:54