我是Python的新手,目前正在学习函数。我创建了一个简单的程序,询问用户一些信息,然后打印出来。但是,我在将return
变量从一个函数链接到另一函数中的print()
语句时遇到问题。
这是我到目前为止的内容:
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()
我的问题是我可以输入所有信息,这意味着ask_data()
在工作。但是,当调用print_data()
时,出现错误,提示找不到变量name
,last_name
和age
。如果出现问题,如何链接两个功能?
答案 0 :(得分:0)
您已经关闭。要记住的是,在main
函数中,变量data
引用了一个元组,因此在print_data
中,您实际上不能引用name
,last_name
,和age
直接。您必须先“解包”元组中的值,如下所示:
def print_data(data):
name, last_name, age = data
print("Your name is ", name, last_name)
print("and you are ", age, "years old")
要记住的是,在函数中声明的变量保留在该函数中。 name
中的变量ask_data
保留在ask_data
中。您将所有值包装在一个元组中并返回该值,然后将该元组传递给print_data
(很好!),但是print_data
不知道这些值的原始名称是正确的。您必须在print_data
内创建 new 变量,因为局部变量的名称对于定义它们的每个函数都是“私有的”。
答案 1 :(得分:0)
因为您在data
和函数def print_data()
中的变量不知道:名称,姓氏和 age
示例:
def print_data(data):
print("Your name is ", data[0], data[1])
print("and you are ", data[2], "years old")
其中data [0]是您的名称,data [1]是 last_name 和data [2]中的最后一个 age 。
答案 2 :(得分:0)
问题是运行脚本时出现错误NameError: name 'name' is not defined
。这是因为您需要执行元组拆包。您可以在this Stack Overflow question中找到出色的示例。
要使脚本正常工作,您可以在任何阶段执行解压缩。最简单的修改如下:
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()
产生完全相同结果的另一种方法如下:
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()
答案 3 :(得分:0)
您可以在函数中使用参数解压缩。看看here。
您将获得以下内容:
def print_data(name, last_name, age):
...
def main():
data = ask_data()
print_data(*data)
答案 4 :(得分:0)
我会使用全局语句
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()
答案 5 :(得分: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