[英]How do I output variables from a function in python?
So I was developing a python program for my school project that asks a customer for their details such as their Firstname,Lastname,Age etc. So I made a function called customer details.所以我正在为我的学校项目开发一个 python 程序,该程序要求客户提供他们的详细信息,例如他们的名字、姓氏、年龄等。所以我制作了一个名为客户详细信息的 function。
def customerdetails():
Firstname = input("Enter your First name:")
Lastname = input("Enter your last name:")
Age = input("Age:")
Address =input("Enter your address:")
Postcode = input("Enter your Postcode:")
Email = input("Email:")
Phone = int(input("Phone Number:"))
customerdetails()
Now how can I print those variables such as Firstname, Lastname, Age, Address etc. I tried using the same logic we use to print normal variables, but it didn't work.This is the code.现在如何打印名字、姓氏、年龄、地址等变量。我尝试使用与打印普通变量相同的逻辑,但它不起作用。这是代码。
print("Please check your details")
print("***********")
print(Firstname)
print("***********")
It shows me an error that says “NameError: name 'Firstname' is not defined.”它向我显示了一个错误,上面写着“NameError: name 'Firstname' is not defined。”
What do I do?我该怎么办? Any help will be appreciated任何帮助将不胜感激
You need to return the information from your customerdetails
function.您需要从您的customerdetails
详细信息 function 返回信息。 Since you have a bunch of different variables, you'll be returning a collection -- a tuple, a list, a dict, etc.因为你有一堆不同的变量,你将返回一个集合——一个元组、一个列表、一个字典等。
Using a tuple or a list means you need to keep track of the order of all the elements, and using a dict means you need to keep track of the exact string literals that correspond to each variable.使用元组或列表意味着您需要跟踪所有元素的顺序,使用 dict 意味着您需要跟踪与每个变量对应的确切字符串文字。 Personally, I prefer using NamedTuple
for simple collections like this;就个人而言,我更喜欢像这样对简单的 collections 使用NamedTuple
; it makes for code that's easy to read and type-check.它使代码易于阅读和类型检查。
from typing import NamedTuple
class CustomerDetails(NamedTuple):
first_name: str
last_name: str
age: str
address: str
postcode: str
email: str
phone: int
def get_customer_details() -> CustomerDetails:
"""Get customer details from the user."""
return CustomerDetails(
input("Enter your First name:"),
input("Enter your last name:"),
input("Age:"),
input("Enter your address:"),
input("Enter your Postcode:"),
input("Email:"),
int(input("Phone Number:")),
)
details = get_customer_details()
print("Please check your details")
print("***********")
print(details.first_name)
print("***********")
By default, variables declared within a function are local only to that function.默认情况下,在 function 中声明的变量仅对 function 是本地的。
If you want a function to give back multiple values, you can return
a tuple of values:如果您希望 function 返回多个值,则可以return
值元组:
def customerdetails():
Firstname = input("Enter your First name:")
Lastname = input("Enter your last name:")
Age = input("Age:")
Address =input("Enter your address:")
Postcode = input("Enter your Postcode:")
Email = input("Email:")
Phone = int(input("Phone Number:"))
return (Firstname, Lastname, Age, Address, Postcode, Email, Phone)
(fn, ln, age, addr, postcode, email, phone) = customerdetails()
I think you propably wrote the print-functions into another function than the function you defined the variable Firstname etc.我认为您可以将打印功能写入另一个 function,而不是您定义变量 Firstname 等的 function。
def customerdetails():
Firstname = input("Enter your First name:")
Lastname = input("Enter your last name:")
Age = input("Age:")
Address =input("Enter your address:")
Postcode = input("Enter your Postcode:")
Email = input("Email:")
Phone = int(input("Phone Number:"))
return Firstname, Lastname, Age,Address, Postcode, Email, Phone
## Calling function
Firstname, Lastname, Age,Address, Postcode, Email, Phone = customerdetails()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.