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

Passing a string variable between two functions in Python

user1075
1#
user1075 Published in July 18, 2018, 10:20 pm

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?

You need to login account before you can post.

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

© 2016 Powered by mzan.com design MATCHINFO