简体   繁体   English

如何从 python 中的 function 中 output 变量?

[英]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.

相关问题 如何使用python中另一个函数的局部变量? - How do I use local variables from another function in python? 我如何从 Python 函数返回多个变量 - How do i return multiples variables from Python function 如何在Python中向函数传递大量变量? - How do I pass lots of variables to and from a function in Python? 如何从 Python 中的 input() 函数获取多字符输出 - How do I get a multicharacter output from input() function in Python 如何定义可与不同变量一起使用并在python中产生输出的函数? - How do I define a function that can be used with different variables and produce an output in python? 如何使用for循环将函数输出存储在单独的变量中? - How do I use a for loop to store function output in separate variables? 从Python函数获取输出变量 - Obtaining output variables from Python function 如何将变量从一个 function 传递到另一个,并在 python 的条件语句中使用它们? - How do I pass variables from one function to another and use them in conditional statements in python? 如何将变量中的偏差值合并到 function 中? [Python] - How do I incorporate the bias values from my variables into my function? [python] 如何将多个变量传递给python中的函数? - How do I pass multiple variables to a function in python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM