Not sure of what I did wrong here
Question:
class Person:
def __innit__(self, name, address, telephone_number):
self.name = name
self.address = address
self.telephone_number = telephone_number
self.Customer(self, name, address, telephone_number)
def Customer(self, name, address, telephone_number):
self.name = name
self.address = address
self.telephone_number = telephone_number
def main():
print('Would you like to be put on the customer list? [y/n] ')
a = input()
if a == "y":
customer_name = input('Please enter your name for the customer list: ')
customer_address = input('Please enter your address for the customer list: ')
customer_telephone_number = input('Please enter your phone number for the customer list: ')
customer_info = Customer(customer_name, customer_address, customer_telepehone_number)
print("Your name is " + customer_info.name, + " you address is " + customer_info.address, + " and your phone number is " + customer_info.telephone_number, + "." )
else:
person_name = input('Please enter your name for the regular list: ')
person_address = input('Please enter your address for the regular list: ')
person_telephone_number = input('Please enter your phone number for the regular list: ')
person_info = Person(person_name, person_address, person_telephone_number)
print("Your name is " + person_info.name, + " you address is " + person_info.address, + " and your phone number is " + person_info.telephone_number, + "." )
main()
I am trying to Write a class named Person with data attributes for a person’s name, address, and telephone number. Next, write a class named Customer that is a subclass of the Person class. The Customer class should have a data attribute for a customer number and a Boolean data attribute indicating whether the customer wishes to be on a mailing list. Demonstrate an instance of the Customer class in a simple program.
Answers:
There are a few issues that stand out. But I would start by suggesting you include error messages in future questions, preferably in a code block like your example.
To start, your dunder method is spelled incorrectly. It should be __init__
, with 1 ‘n’. Otherwise, your code is rather British, innit? (Apologies to any Brits if I used that incorrectly). init
is short for initialize, as it is the code ran when a new object is created, it initializes the new object. new is another dunder method that can be implemented, that one actually creates and returns the initialized object, but for now you can pretend it doesn’t exist until you have a usecase where you need to customize the creation of the object. Fix that typo and the dunder method will work correctly.
Also, you define customer as a function. To me, it looks like you could benefit from using inheritance, or a factory pattern (I’d worry about object oriented design patterns once you are mkre comfortable, so inheritance would be a simpler approach to start). That would look something like:
class Person:
# Person class implementation here
class Customer(Person):
def __init__(self, name, address, telephone_number, <any_additional_arguments_needed_by_subclass>):
super().__init__(name, address, telephone_number)
# rest of Customer class implementation
If you insist on not changing your code (I’d recommend working through one of the free beginner intros to python on YouTube, then revisiting this) you could make it work by creating a Person object then calling the function.
Just like to include the reason your Customer function doesn’t work is because it is not accessible outside of an instance of Person. You would have to first create a Person, then call the Customer function from that instance (synonymous with object, you’ll see it referred to as either an instance of a class or a class object in literature).
You seem early in your journey. Stay curious, you’ll get there. Then you’ll realize we all bumble our way through with Google, lol.
Let me give you some pointers on how to improve your implementation:
class Person:
def __init__(self, name, address, telephone_number):
# this class definition is ok, good job!
self.name = name
self.address = address
self.telephone_number = telephone_number
# just removed the call to Customer from here
# your class Customer derives from Person (so it already has all the attributes of Person)
class Customer(Person):
def __init__(self, name, address, telephone_number, customer_id, mailing):
# with super, you are basically initalizing the fields that Customer has in common with Person
super().__init__(name, address, telephone_number)
# the new attributes are then initialized in Customer
self.customer_id = customer_id
self.mailing = mailing
def main():
# I refactored the code to make it clear that Person is the base of Customer
# First, getting the fields that are common
name = input('Please enter your name: ')
address = input('Please enter your address: ')
telephone_number = input('Please enter your phone number: ')
# now deciding if it would be a Customer or not
is_customer = input('Would you like to become a customer? [y/n] ')
if is_customer == "y":
customer_id = 1234567 # or whatever method used to create a unique customer id
mailing_input = input('Would you like to be put on a mailing list? [y/n] ')
mailing = (mailing_input == "y") # this will become True if the answer is y, otherwise False
info = Customer(name, address, telephone_number, customer_id, mailing)
else:
info = Person(name, address, telephone_number)
# info can be either a Person or a Cusromer. In any case, basic information is present
print(f"Your name is {info.name}, your address is {info.address} and your phone number is {info.telephone_number}.")
if type(info) == Customer:
# now that we are sure the info is a Customer, we can access exclusive attributes
print(f"Your customer id is {info.customer_id}, you want to be on a mailing list? {info.mailing}.")
main()
class Person:
def __innit__(self, name, address, telephone_number):
self.name = name
self.address = address
self.telephone_number = telephone_number
self.Customer(self, name, address, telephone_number)
def Customer(self, name, address, telephone_number):
self.name = name
self.address = address
self.telephone_number = telephone_number
def main():
print('Would you like to be put on the customer list? [y/n] ')
a = input()
if a == "y":
customer_name = input('Please enter your name for the customer list: ')
customer_address = input('Please enter your address for the customer list: ')
customer_telephone_number = input('Please enter your phone number for the customer list: ')
customer_info = Customer(customer_name, customer_address, customer_telepehone_number)
print("Your name is " + customer_info.name, + " you address is " + customer_info.address, + " and your phone number is " + customer_info.telephone_number, + "." )
else:
person_name = input('Please enter your name for the regular list: ')
person_address = input('Please enter your address for the regular list: ')
person_telephone_number = input('Please enter your phone number for the regular list: ')
person_info = Person(person_name, person_address, person_telephone_number)
print("Your name is " + person_info.name, + " you address is " + person_info.address, + " and your phone number is " + person_info.telephone_number, + "." )
main()
I am trying to Write a class named Person with data attributes for a person’s name, address, and telephone number. Next, write a class named Customer that is a subclass of the Person class. The Customer class should have a data attribute for a customer number and a Boolean data attribute indicating whether the customer wishes to be on a mailing list. Demonstrate an instance of the Customer class in a simple program.
There are a few issues that stand out. But I would start by suggesting you include error messages in future questions, preferably in a code block like your example.
To start, your dunder method is spelled incorrectly. It should be __init__
, with 1 ‘n’. Otherwise, your code is rather British, innit? (Apologies to any Brits if I used that incorrectly). init
is short for initialize, as it is the code ran when a new object is created, it initializes the new object. new is another dunder method that can be implemented, that one actually creates and returns the initialized object, but for now you can pretend it doesn’t exist until you have a usecase where you need to customize the creation of the object. Fix that typo and the dunder method will work correctly.
Also, you define customer as a function. To me, it looks like you could benefit from using inheritance, or a factory pattern (I’d worry about object oriented design patterns once you are mkre comfortable, so inheritance would be a simpler approach to start). That would look something like:
class Person:
# Person class implementation here
class Customer(Person):
def __init__(self, name, address, telephone_number, <any_additional_arguments_needed_by_subclass>):
super().__init__(name, address, telephone_number)
# rest of Customer class implementation
If you insist on not changing your code (I’d recommend working through one of the free beginner intros to python on YouTube, then revisiting this) you could make it work by creating a Person object then calling the function.
Just like to include the reason your Customer function doesn’t work is because it is not accessible outside of an instance of Person. You would have to first create a Person, then call the Customer function from that instance (synonymous with object, you’ll see it referred to as either an instance of a class or a class object in literature).
You seem early in your journey. Stay curious, you’ll get there. Then you’ll realize we all bumble our way through with Google, lol.
Let me give you some pointers on how to improve your implementation:
class Person:
def __init__(self, name, address, telephone_number):
# this class definition is ok, good job!
self.name = name
self.address = address
self.telephone_number = telephone_number
# just removed the call to Customer from here
# your class Customer derives from Person (so it already has all the attributes of Person)
class Customer(Person):
def __init__(self, name, address, telephone_number, customer_id, mailing):
# with super, you are basically initalizing the fields that Customer has in common with Person
super().__init__(name, address, telephone_number)
# the new attributes are then initialized in Customer
self.customer_id = customer_id
self.mailing = mailing
def main():
# I refactored the code to make it clear that Person is the base of Customer
# First, getting the fields that are common
name = input('Please enter your name: ')
address = input('Please enter your address: ')
telephone_number = input('Please enter your phone number: ')
# now deciding if it would be a Customer or not
is_customer = input('Would you like to become a customer? [y/n] ')
if is_customer == "y":
customer_id = 1234567 # or whatever method used to create a unique customer id
mailing_input = input('Would you like to be put on a mailing list? [y/n] ')
mailing = (mailing_input == "y") # this will become True if the answer is y, otherwise False
info = Customer(name, address, telephone_number, customer_id, mailing)
else:
info = Person(name, address, telephone_number)
# info can be either a Person or a Cusromer. In any case, basic information is present
print(f"Your name is {info.name}, your address is {info.address} and your phone number is {info.telephone_number}.")
if type(info) == Customer:
# now that we are sure the info is a Customer, we can access exclusive attributes
print(f"Your customer id is {info.customer_id}, you want to be on a mailing list? {info.mailing}.")
main()