How to spot my error in Python? - python-3.x

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.'
This is what I have for my code, but I keep getting the following error:
Traceback (most recent call last):
File "/Users/ryanraben/Desktop/person1.py", line 45, in <module>
import customer
ModuleNotFoundError: No module named 'customer'
I have been struggling with this class this semester. I found someone asking a similar question here in Stack Overflow but their code was very different than what I had (and if I copied their code I still could not get the correct results). This was a video module and I entered my code as it appeared on the instructor's screen, but obviously I didn't do it right because his code works and mine does not.
class Person:
def __init__(self, name, address, phone):
self.__name = name
self.__address = address
self.__phone = phone
def set_name (self, name):
self.__name = name
def set_address (self, address):
self.__address = address
def set_phone (self, phone):
self.__phone = phone
def get_name (self):
return self.__name
def get_address (self):
return self.__address
def get_phone (self):
return self.__phone
class Customer (Person):
def __init__(self, name, address, phone, customer_number, mailing_list):
Person.__init__(self, name, adress, phone)
self.__customer_number = customer_number
self.__mailing_list = mailing_list
def set_customer_number (self, customer_number):
self.__customer_number = customer_number
def set_mailing_list(self, mailing_list):
self.__mailing_list = mailing_list
def get_customer_number(self):
return self.__customer_number
def get_mailing_list (self):
return self.__mailing_list
import customer
name = input ('Name: ')
address = input ('Address: ')
phone = input ('Phone: ')
customer_number = input ('Customer number: ')
mail = input ('Include in mailing list? (y/n): ')
if mail.lower()=='y':
mailing_list = True
else:
mailing_list = False
my_customer = customer.Customer (name, address, phone, customer_number, mailing_list)
print ('Customer Information')
print ('-----------------------------')
print ('Name: ', my_customer.get_name())
print ('Address: ', my_customer.get_address())
print ('Phone: ', my_customer.get_phone())
print ('Customer number: ', my_customer.get_customer_number())
print ('Mailing list: ', my_customer.get_mailing_list())

This is a pretty common error:
You're attempting to import the library of customer and your IDE is simply not able to find this file.
Since you're defining the Class Customer I can't see any reason why you would need to import this non-existant library.
Consequently, I suggest you delete the line import customer.
That is unless I've misunderstood something.

remove this line(import customer) and remove customer from 'customer.Customer' while creating the object. After all of this you have error of 'adress' not defined. Find this attribute in your code and change it to address. Thats All dear.

Related

How do I return values of one class method to another class method?

I am new to Classes and want to know how to return values from one class method to another. Below is the code and I want to return the queues from arrive() function to next_customer() function.
class DonutQueue():
def __init__(self):
self.queue=[]
self.queue2= []
def arrive(self,name,vip):
self.name = name
self.vip = vip
if self.vip==1:
self.queue2.append(self.name)
return self.queue2
else:
self.queue.append(self.name)
return self.queue
def next_customer(self):
while not self.queue2== []:
if not self.queue2==[]:
return self.queue
else:
return self.queue2
def main():
n = int(input("Enter the number of customers you want to add"))
for i in range(0,n):
name = input("Enter their name")
vip= int(input("Are they a VIP"))
DonutQueue().arrive(name,vip)
print(DonutQueue().next_customer())
If I add the below statement in the next_customer function, I get the mentioned error:
a = self.arrive(self.name,self.vip)
Error:
AttributeError: 'DonutQueue' object has no attribute 'name'
The problem is when I call next_customer() it returns None because I am getting values from init instead of arrives()
in python you have to define members in the __init__ function means. in your case you defined the self.name member only in arrive thats why its not available (and throws an exception) in your case.

How to remove an object from list by a value

My problem is that I created a list of students with name and number. The task is now to remove a student by his number. My problem is that my code doesn't work.
Another problem is that it always shows the memory address instead of the value of the object.
Thanks in advance
class Student:
def __init__(self, name, number):
self.name = name
self.number = number
from .student import Student
class Course:
def __init__(self, name, code, credit, student_limit):
self.name = name
self.code = code
self.credit = credit
self.student_limit = student_limit
students = []
def add_student(self, new_student):
self.student = new_student
self.students.append(new_student)
print("Student added" +str(self.students))
def remove_student_by_number(self, student_number):
self.student_number = student_number
if student_number in self.students: self.students.remove(student_number)
print("Student removed" + str(self.students))
from .course import Course
class Department:
def __init__(self, name, code):
self.name = name
self.code = code
courses = []
def add_course(self, course):
self.course = course
self.courses.append(course)
print("Course added" +str(self.courses))
from python import *
def main():
alice = Student("Alice", 1336)
bob = Student("Bob", 1337)
math_dept = Department("Mathematics and Applied Mathematics", "MAM")
math_course = Course("Mathematics 1000", "MAM1000W", 1, 10)
math_dept.add_course(math_course)
math_course.add_student(bob)
math_course.add_student(alice)
math_course.remove_student_by_number(alice.number)
if __name__ == "__main__":
main()
self.students is a list of Student instance so it will print the instance's memory address if the method __str__ is not implemented (see here for example). You should try to print each property like student.name or student.number explicitly.
Anyway you are trying to find student_number in list of Student so of course it will never run the remove line. Instead use if student_number in [student.number for student in self.students] which is looking up the student's number list, not the student list itself. However in this case, you also want to remove the student with the student_number as the input so you may need to use a loop like this:
def remove_student_by_number(self, student_number):
for student in self.students:
if student.number == student_number:
print("Student removed" + str(student.name))
self.students.remove(student)
break

How to connect patient class with doctor class?

so I'm working on a problem I found online just to practice my python coding skills and I got stuck somewhere along the line. So I was trying to list all the patient name with the one of the class definition called print_list_of_patients. However, I don't know how to link the patient with the attribute so that I could just refer to the name based on the date of their appointment
I tried to combine each patient into tuples, dict but I still couldn't get what I'm looking for. Basically, I was trying to group the patient class so that every time I look at the appointment date, I can access their name and their sickness as well.
class Doctor:
def __init__(self, name):
self.name = name
self.list_of_patient = []
self.availability = []
def __str__(self):
return "Hi, this is Dr. {a}".format(a=self.name)
def add_patient(self, patient):
self.list_of_patient.
def remove_patient(self, patient):
for dates in self.availability:
if len(self.list_of_patient) == 0 and self.availability[dates]:
print("There is no patient left")
else:
self.list_of_patient.remove(patient.name)
def add_days(self, *dates):
self.availability.append(dates)
def remove_days(self, *date):
for num in range(len(self.availability)):
if date == self.availability:
self.availability.remove(date)
def print_list_of_patient(self, date):
print("Dr. {a} patient's list for {b}:\n".format(a=self.name, b=date))
class Patient:
def __init__(self, name, sickness, date_of_appointment):
self.name = name
self.sickness = sickness
def __str__(self):
return "Name: {a}\n Sickness: {b}\n\n".format(a=self.name, b=self.sickness)
def set_up_appointment(doctor, patient):
"""
set_up_appointment(doctor, patient, date)
doctor = variable name (unique ID)
patient = variable name (unique ID)
:return:
"""
doctor.add_patient(patient)
def cancel_an_appointment(doctor, patient):
"""
cancel_an_appointment(doctor, patient)
doctor = variable name (unique ID)
patient = variable name (unique ID)
:return:
"""
for names in range(len(doctor.number_of_patient)):
if patient.name == doctor.number_of_patient[names].name:
doctor.remove_patient()

python class error str object has no attribute (beginner)

Hi I'm supposed to create knows and meet functions of this class but it keeps giving me different errors, so how I'm supposed to actually do it? Mostly I would like to know how to implement the knows function so I can finish the rest myself. Here's the code
class Person(object):
'''
The Person class implements a single person in network of people knowing each other.
'''
def __init__(self, name):
'''
initializes a new person with the given name. The person initially has no friends.
#param name: the name of the person created.
'''
self.name = name
self.friend = None
def get_friend(self):
'''
Returns the immediate friend of this person.
#return: the immediate friend (person-object) of this person.
'''
return self.friend
def set_friend(self, friend):
'''
Sets the immediate friend of this person.
'''
self.friend = friend
def knows(self, other):
'''
Tests if a person is connected to another by a path.
#param other: the person-object in the other end of the path tested.
#return: true if the persons are connected, false otherwise.
'''
if self.name==None:
return False
while self.name.get_friend() != self.name or self.name.get_friend() != None:
self.name=self.name.get_friend()
while other.get_friend(other) != other.get_name() or other.get_friend() != None:
other=other.get_friend()
if self.get_name() == other.get_name():
return True
else:
return False
# please implement me
def meet(self, other):
'''
Tries to add a path connecting to persons.
#param other: the other end of the connection being introduced (person-object).
#return: true if and only if the persons were not previously connected.
'''
# please implement me
if self.knows(other)==True:
return False
else:
while self.name.get_friend() != self.get_name() or self.get_name() != None:
self.name = self.get_friend()
while other.get_friend() != other.get_name() or other.get_friend() != None:
other=other.get_friend()
self.name.set_friend(other.get_name)
return True
def get_name(self):
'''
Returns a string representation of this Person, containing the name of the person.
#return: the name of the person.
'''
return self.name
I ran your code, and it executed without an error. My guess is that other is a string, and in meet() and knows(), you call other.get_friend(), which is obviously undefined for strings.
>>> mystring = ' '
>>> mystring.get_friend()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'str' object has no attribute 'get_friend'
>>>
In réponse to your comment
The reason you are getting NoneType is because you are assigning other.get_friend() to other, which assigns other to self.friend, which you set to None in your __init__. Try calling set_friend before you call other=other.get_friend().

Python printing a object

In the below code I need to print the contactlist object..How do I do that?
# Test.py
class ContactList(list):
def search(self, name):
'''Return all contacts that contain the search value
in their name.'''
matching_contacts = []
for contact in self:
if name in contact.name:
matching_contacts.append(contact)
return matching_contacts
class Contact:
all_contacts = ContactList()
def __init__(self, name, email):
self.name = name
self.email = email
self.all_contacts.append(self)
I have created 2 object of Contact but want to see all the elements in the all_contacts list..
How about:
print(Contact.all_contacts)
or:
for c in Contact.all_contacts:
print("Look, a contact:", c)
To control how the Contact prints, you need to define a __str__ or __repr__ method on the Contact class:
def __repr__(self):
return "<Contact: %r %r>" % (self.name, self.email)
or, however you want to represent the Contact.

Resources