1

I'm pretty new to Python and I'm learning about functions at the moment. I have created a simple program that asks the user for some information and then prints it. However, I'm having problems linking the return variables from one function to the print() statements in the other function. This is what I have so far:

def ask_data():
    name = input("What's your name?: ")
    last_name = input("What's your last name?: ")
    age = input("how old are you?: ")
    return(name, last_name, age)

def print_data(data):
    print("Your name is ", name, last_name)
    print("and you are ", age, "years old")

def main():
    data = ask_data()
    print_data(data)

main()

My problem is that I'm able to enter all the information, meaning that ask_data() is working. However, when print_data() is called, I get an error saying the variables name, last_name, and age couldn't be found. How can I link both functions, if that's the issue?

Me All
  • 269
  • 1
  • 5
  • 17

6 Answers6

0

You're close. The thing to remember is in your main function, the variable data refers to a tuple, so within print_data you actually cannot refer to name, last_name, and age directly. You must first "unpack" the values in the tuple like so:

def print_data(data):
    name, last_name, age = data
    print("Your name is ", name, last_name)
    print("and you are ", age, "years old")

The thing to remember is variables declared in a function stay in that function. The variable name inside of ask_data stays in ask_data. You were correct to wrap up all the values in a tuple and return that, then pass that tuple to print_data (that's good!) but print_data doesn't know the original names of those values. You have to make new variables inside of print_data, because the names of local variables are "private" to each function that defines them.

Ray Toal
  • 86,166
  • 18
  • 182
  • 232
0

because your variables in data and function def print_data() does not know about: name, last_name and age.

Example:

def print_data(data):
    print("Your name is ", data[0], data[1])
    print("and you are ", data[2], "years old")

Where data[0] it is youre name, data[1] is last_name and last var age in data[2].

0

The problem is that you get the error NameError: name 'name' is not defined when you run your script. This is because you need to perform tuple unpacking. You can find great examples in this Stack Overflow question.

To make your script work, you can perform the unpacking at any stage. The simplest modification would be as follow:

def ask_data():
    name = input("What's your name?: ")
    last_name = input("What's your last name?: ")
    age = input("how old are you?: ")
    return(name, last_name, age)

def print_data(data):
    # This will unpack 'data' and make those variables available
    name, last_name, age = data
    print("Your name is ", name, last_name)
    print("and you are ", age, "years old")

def main():
    data = ask_data()
    print_data(data)

main()

One other approach, which produces the exact same result, would be as follow:

def ask_data():
    name = input("What's your name?: ")
    last_name = input("What's your last name?: ")
    age = input("how old are you?: ")
    return(name, last_name, age)

def print_data(name, last_name, age):  # Giving the function new parameters
    print("Your name is ", name, last_name)
    print("and you are ", age, "years old")

def main():
    name, last_name, age = ask_data()  # Unpacking outside the function
    print_data(name, last_name, age)

main()
Sébastien Lavoie
  • 877
  • 1
  • 11
  • 18
0

You can use arguments unpacking in your function. Have a look here.

You would obtain something like this:

def print_data(name, last_name, age):
    ...

def main():
    data = ask_data()
    print_data(*data)
mathdugre
  • 98
  • 7
0

i would use the global statement

def ask_data():
    global name, last_name, age
    name = input("What's your name?: ")
    last_name = input("What's your last name?: ")
    age = input("how old are you?: ")
    return(name, last_name, age)

def print_data(data):
    global name, last_name, age
    print("Your name is ", name, last_name)
    print("and you are ", age, "years old")

def main():
    global name, last_name, age
    data = ask_data()
    print_data(data)

main()
3NiGMa
  • 545
  • 1
  • 9
  • 24
0
def ask_data():
    return name, last_name, age
name = input("What's your name?: ")           # i indented your variable to the same line as the funtion "ask_data" 
last_name = input("What's your last name?: ") #reason: so that any of your funtion (e.g. print_data) below can access them
age = input("how old are you?: ")             # you can bring these variable above "def ask_data" if you want.

def print_data(data):                         # in this program you don't need the 1 argument "data". "print_data()" is enough
    print("Your name is ", name, last_name)  # reason: data is not defined, 
    print("and you are ", age, "years old")

def main():
    ask_data()
    print_data("data") # because the word data has not been define, (your can only use it as a string to call print_data
                     # like this print_data("data"). if it was defined, then you can call it as a variable without " ".

main()


my input:
       What's your name?: Simon
       What's your last name?: cigar
       how old are you?: 35

output:
       Your name is  Simon cigar
       and you are  35 years old
Vuga
  • 23
  • 3