Home Passing a string variable between two functions in Python
Reply: 3

Passing a string variable between two functions in Python

MathiusBacharus
1#
MathiusBacharus Published in 2017-12-06 14:43:44Z

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?

quamrana
2#
quamrana Reply to 2017-12-06 14:53:52Z

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()
Patrick Artner
3#
Patrick Artner Reply to 2017-12-06 14:47:30Z
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

Mike
4#
Mike Reply to 2017-12-06 18:12:50Z

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
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.304587 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO