You are viewing our Forum Archives. To view or take place in current topics click here.
Help on my python lab project
Posted:
Help on my python lab projectPosted:
Status: Offline
Joined: Aug 03, 201212Year Member
Posts: 353
Reputation Power: 17
Status: Offline
Joined: Aug 03, 201212Year Member
Posts: 353
Reputation Power: 17
This is for one of my assignments for my intro programming class and believe I have done this correctly. We just started classes and object orienting programming. Could someone clarify that this is done correctly? I'm mainly wondering about the part of the inheritance of classes.
-Assignment
-And this is my code. If there are any corrections that you can think of, could you please tell me?
Thanks!
Last edited by TastyCashews ; edited 1 time in total
-Assignment
-Create a Person class that provides attributes for first name, last name, and email address. This
class should provide a property or method that returns the person's full name.
-Create a Customer class that inherits the Person class. This class should add an attribute for a
customer number.
-Create an Employee class that inherits the Person class. This class should add an attribute for a
social security number (SSN).
-The program should create a Customer or Employee object from the data entered by the user, and
it should use this object to display the data to the user. To do that, the program can use the
isinstance() function to check whether an object is a Customer or Employee object.
class should provide a property or method that returns the person's full name.
-Create a Customer class that inherits the Person class. This class should add an attribute for a
customer number.
-Create an Employee class that inherits the Person class. This class should add an attribute for a
social security number (SSN).
-The program should create a Customer or Employee object from the data entered by the user, and
it should use this object to display the data to the user. To do that, the program can use the
isinstance() function to check whether an object is a Customer or Employee object.
-And this is my code. If there are any corrections that you can think of, could you please tell me?
Thanks!
#!/usr/bin/env python3
class Person:
def __init__(self,first,last,email):
self.first = first
self.last = last
self.email = email
def get_first(self):
return self.first
def get_last(self):
return self.last
def get_email(self):
return self.email
def get_full_name(self):
return self.first , self.last
class Employee(Person):
def __init__(self,first,last,email,number):
super().__init__(first,last,email)
self.number = number
def get_first(self):
return self.first
def get_last(self):
return self.last
def get_email(self):
return self.email
def get_number(self):
return self.number
class Customer(Person):
def __init__(self,first,last,email,number):
super().__init__(first,last,email)
self.number = number
def get_first(self):
return self.first
def get_last(self):
return self.last
def get_email(self):
return self.email
def get_number(self):
return self.number
def e_data_entry():
print("\nDATA ENTRY")
first = input("First name: ")
last = input("Last name: ")
email = input("Email: ")
number = input("SSN: ")
emp = Employee(first,last,email,number)
print("\nEMPLOYEE")
print("First name: ", emp.get_first())
print("Last name: ", emp.get_last())
print("Email: ", emp.get_email())
print("SSN: ", emp.get_number())
def c_data_entry():
print("\nDATA ENTRY")
first = input("First name: ")
last = input("Last name: ")
email = input("Email: ")
number = input("Number: ")
cus = Customer(first,last,email,number)
print("\nCUSTOMER")
print("First name: ", cus.get_first())
print("Last name: ", cus.get_last())
print("Email: ", cus.get_email())
print("Number: ", cus.get_number())
def main():
print("Customer/Employee Data Entry\n")
again = "y"
while again != "n":
choice = input("Customer or employee? (c/e): ")
choice = choice.lower()
if choice == "e":
e_data_entry()
elif choice == "c":
c_data_entry()
else:
print("\nNot an option. Please try again.")
continue
while True:
again = input("\nContinue? (y/n): ")
again = again.lower()
if again == "y":
print()
break
elif again == "n":
print("\nBye!")
break
else:
print("\nNot an option. Please try again.")
if __name__ == "__main__":
main()
Last edited by TastyCashews ; edited 1 time in total
#2. Posted:
Status: Offline
Joined: Jul 21, 201212Year Member
Posts: 248
Reputation Power: 460
Status: Offline
Joined: Jul 21, 201212Year Member
Posts: 248
Reputation Power: 460
Think you're going to have to embed your code before anyone even reads that!
- 0useful
- 0not useful
#3. Posted:
Status: Offline
Joined: Aug 03, 201212Year Member
Posts: 353
Reputation Power: 17
Status: Offline
Joined: Aug 03, 201212Year Member
Posts: 353
Reputation Power: 17
Evoque wrote Think you're going to have to embed your code before anyone even reads that!
Man, I feel like a rookie... It's fixed now.
- 0useful
- 0not useful
#4. Posted:
Status: Offline
Joined: Oct 09, 201014Year Member
Posts: 2,358
Reputation Power: 106
Status: Offline
Joined: Oct 09, 201014Year Member
Posts: 2,358
Reputation Power: 106
at a quick glance you're recreating methods from parent classes which defeats the purpose of inheritance.
The employee method is unnecessary as its inherited from Person
class Person:
def get_first(self):
return self.first
class Employee(Person):
def get_first(self):
return self.first
The employee method is unnecessary as its inherited from Person
- 1useful
- 0not useful
You are viewing our Forum Archives. To view or take place in current topics click here.