Write a class named 'Person' with data attributes for a person's name, address, and telephone number.
class Person:
def __init__(self, n, a, p):
self.name = n
self.address = a
self.phone = p
def set_name(self, n):
self.name = n
def set_address(self, a):
self.address = a
def set_phone(self, p):
self.phone = p
def get_name(self):
return 'Customer Name:'
def get_address(self):
return 'Customer Address:'
def get_phone(self):
return 'Customer Phone Number:'
def main():
n = input('Enter Customer Name: ')
a = input('Enter the Customer Address: ')
p = input('Enter the Customer Phone Number: ')
print(n.get_person())
print(a.get_address())
print(p.get_phone())
main()
How am I able to fix my attribute error?
CodePudding user response:
First, your errors really should be TypeError
, because you are calling an instance method from the class:
>>> class Foo:
... def __init__(self):
... self.bar = 1
... def thing(self):
... return self.bar
...
>>>
>>>
>>> Foo.thing()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: thing() missing 1 required positional argument: 'self'
However, the fix is the same regardless.
You need to create an instance of a class to access its attributes:
class Foo:
def __init__(self):
self.bar = 1
# Foo is a class, but it doesn't have a
# bar attribute. An *instance* of the class
# does
# this raises an attributeError
Foo.bar
AttributeError
# this doesn't, because we created an instance
foo = Foo()
foo.bar
1
You need to create a person instance, then you can get its attributes:
class Person:
def __init__(self, name, address, phone):
self.name = name
self.address = address
self.phone = phone
Person.phone
# AttributeError
# Create an instance of Person here
tim = Person(name='Tim', address='1234 Main St', phone='555-5555')
# Then you can get Tim's information
tim.phone
'555-5555'
CodePudding user response:
You need to create an instance of the Person class using something like person = Person(n, a, p)
. Do this after the input statements of your main()
function.
Secondly, when printing out the attributes, you need to reference the instance of the Person class that you created (ie. person.get_person(), person.get_address(), etc).
Finally, when calling the get function, make sure those functions are returning the value that you're looking for. The function get_address()
should return self.address.
Here is my suggestion:
class Person:
def __init__(self, n, a, p):
self.name = n
self.address = a
self.phone = p
def set_name(self, n):
self.name = n
def set_address(self, a):
self.address = a
def set_phone(self, p):
self.phone = p
def get_name(self):
return self.name
def get_address(self):
return self.address
def get_phone(self):
return self.phone
def main():
n = input('Enter Customer Name: ')
a = input('Enter the Customer Address: ')
p = input('Enter the Customer Phone Number: ')
person = Person(n, a, p) # create the instance
# get attributes of instantiated object
print(person.get_person())
print(person.get_address())
print(person.get_phone())
main()