-1

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.

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.”

What do I do ? Any help will be appreciated

4 Answers4

1

You need to return the information from your 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. Personally, I prefer using NamedTuple for simple collections like this; 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("***********")
Samwise
  • 68,105
  • 3
  • 30
  • 44
0

By default, variables declared within a function are local only to that function.

If you want a function to give back multiple values, you can return a tuple of values:

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()
Samuel Hunter
  • 527
  • 2
  • 11
0

I think you propably wrote the print-functions into another function than the function you defined the variable Firstname etc.

Julian
  • 64
  • 6
0
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()
Felix Tenn
  • 130
  • 7