-4

I have some code I messing around with.. Very new to python and coding in general, however I stumped on this one.

I defined a function, and got some user input from that. When I try to print the input, I get a NameError: name 'my_name' is not defined.

Here is the code..

  #New Program
  #Written by Chris Nelson of Cnelson Creations

  #This code obtains some basic information from the user
   def questions():
    if userinput == "Y":
        my_name = input("What is your name? ")`enter code here`
        my_age = input("What is your age? ")
        my_urname = input("Please pick a username.. ")
        my_psword = input("Please pick a password.. ")
    elif userinput == "N":
        print ("Sorry we need the information to continue..")
        print ("... Goodbye")
        exit()
    else:
        print ("Not a valid choice.. Goodbye!")
        exit()
   print ("We need to obtain some information from you before going forward")
   print ("Is this ok? Enter Y for 'yes' or N for 'No'...")
   userinput = input("Please enter Y or N ")
   questions() #This code runs the function listed above..
   print ("Great we have some information from you!")
   print ("Lets confirm it is the correct information.. ")
   print ('{}, {}, {}, {}, is this information correct?'.format(my_name, my_age, my_urname, my_psword))
Christopher Nelson
  • 887
  • 2
  • 17
  • 26

3 Answers3

2

You are defining my_name inside a function which will go into local scope. Look at this question for a Short Description of the Scoping Rules?. You have multiple ways to correct this

  • For my_name and the other variables defined in the function to be visible you will have to define them outside your function.

  • Or add the global declaration inside your function

    global my_name,my_age,my_urname,my_psword

  • Or use the famous Tuple packing and Sequence Unpacking

    Have a statement at the end of your if condition in the function

    return (my_name,my_age,my_urname,my_psword) and the have my_name,my_age,my_urname,my_psword = questions() in your function call.

  • Or print the values in the function itself, Like you have yourself suggested

Community
  • 1
  • 1
Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
0

Its because of that you attempt to print the my_name outside the function . As you defied it inside the function so it is in local (scope) namespace (variables inside a function has local scope (means that you couldn't access them outside the function and on another scopes ))! if you want to access it outside the function you need to define it as global . by adding global my_name at the top level of your function, also as you wanted to access to another variables you need to do something like my_name for those .

But as amore efficient way you could return those variable inside your function :

def questions():
    if userinput == "Y":
        my_name = input("What is your name? ")`enter code here`
        my_age = input("What is your age? ")
        my_urname = input("Please pick a username.. ")
        my_psword = input("Please pick a password.. ")
    elif userinput == "N":
        print ("Sorry we need the information to continue..")
        print ("... Goodbye")
        exit()
    else:
        print ("Not a valid choice.. Goodbye!")
        exit()
    return (my_name,my_age,my_urname,my_psword)

and outside the function you could do the following assignment :

my_name, my_age, my_urname, my_psword= questions()
Mazdak
  • 105,000
  • 18
  • 159
  • 188
-3

Add global my_name,my_age,my_urname,my_psword

def questions():
    global my_name,my_age,my_urname,my_psword
    if userinput == "Y":
        my_name = input("What is your name? ")`enter code here`
        my_age = input("What is your age? ")
        my_urname = input("Please pick a username.. ")
        my_psword = input("Please pick a password.. ")

So you can use that variables outside of function too.

GLHF
  • 3,835
  • 10
  • 38
  • 83
  • There are always better ways than global – Padraic Cunningham Dec 27 '14 at 16:31
  • So should we warn Guido van Rossum about lets remove _global_ from Python ? For example eval() is a dangerous func but if you know how to use it than there is no problem. – GLHF Dec 27 '14 at 16:32
  • But he should learn what _global_ does too, and its a good answer In my opinion, as you see all another answers same. – GLHF Dec 27 '14 at 16:34
  • Sir, Im not arguing. I just saying that, he should learn _global_ if he going to work with functions.. So my answer is with _global_. – GLHF Dec 27 '14 at 16:42
  • Opinions are not need. qqvc's answer is almost the same as Bhargav Rao's answer. They both said that adding a global statement is needed. – Anthony Pham Dec 29 '14 at 17:40
  • 1
    @ Padraic Cunningham, didn't you not read that he said that: "Or add the global declaration inside your function"? – Anthony Pham Dec 29 '14 at 21:53
  • 1
    Why you guys arguing here? What is your problem. Padraic, if you consider answers which one get most upvote then you are really missunderstand this website. There is thousands of answers in SO which they doesnt deserve any up vote, just some fans upvoting them. – GLHF Jan 01 '15 at 03:43