1

I am having trouble passing a variable from one function to another:

def name():

    first_name=input("What is your name? ")

    if len(first_name)==0:
            print("\nYou did not enter a name!")
            return name()

    else:
            print("\nHello %s." % first_name)
            return surname()


def surname():

    last_name=input("\nCan you tell my your last name please?")

    if len(last_name)==0:
            print("\nYou did not enter a last name!")
            return surname()
    else:

            print("Nice to meet you %s %s." % (first_name,last_name))

I want the last command to print the inputed first_name from def name() and last name from def surname()

I always get the error that first_name is not defined and I do not know how to import it from the first function. The error I get is:

    print("Nice to meet you %s %s." % (first_name,last_name))
NameError: name 'first_name' is not defined

What am I doing wrong?

1
  • Chage surname function to be surname(first_name) and call it with return surname(first_name) Commented Dec 6, 2017 at 14:49

3 Answers 3

2

You need to pass the information in the function call:

def name():

    first_name = input("What is your name? ")

    if len(first_name) == 0:
        print("\nYou did not enter a name!")
        return name()

    else:
        print("\nHello %s." % first_name)
        surname(first_name)  # pass first_name on to the surname function


def surname(first_name): #first_name arrives here ready to be used in this function

    last_name = input("\nCan you tell my your last name please?")

    if len(last_name) == 0:
        print("\nYou did not enter a last name!")
        surname(first_name)
    else:
        print("Nice to meet you %s %s." % (first_name,last_name))

name()
Sign up to request clarification or add additional context in comments.

Comments

0
def functionname(untypedparametername):
    # do smth with untypedparametername which holds "Jim" for this example   

name = "Jim"

functionname(name) # this will provide "Jim" to the function

You can see how they are used if you look at the examples in the documentation, f.e. here: https://docs.python.org/3/library/functions.html

Maybe you should read up on some of the tutorials for basics, you can find lots of them on the python main page: https://wiki.python.org/moin/BeginnersGuide

Comments

0

You can also use while loop to ask the names constantly until there is a valid input.

def name_find():
   while True:
       first_name=raw_input("What is your name? ")
       if len(first_name)==0:
           print("\nYou did not enter a name!")
           return name_find()
       else:
           print("\nHello %s." % first_name)
           return surname(first_name)
def surname(first_name):
   while True:
      last_name=raw_input("\nCan you tell me your last name please?")
      if len(last_name)==0:
          print("\nYou did not enter a last name!")
     else:
          print "Nice to meet you %s %s." % (first_name, last_name)
          break

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.