hilal123
hilal123

Reputation: 13

Problem with code in whilst using Classes

 class Customer:
    """The people who are going to be accessing this program"""
    
    def __init__(self, first, last, gender, age):
        self.first = first 
        self.last = last
        self.gender = gender
        self.age = age
    def buying_something(self):
        """The customer is now buying something"""
        print(f"{self} is now buying something")
    def browsing(self):
        """The customer is now browsing something"""
        print(f"{self} is now browsing something")

customer_1 = Customer('John', 'Doe', 'Male', 20)
print(f"This customers name is {customer_1.first}")
print(f"This customers age is {customer_1.age}")
customer_1.buying_something()

This is some code that I have done to try and implement what I have learned so far on Classes. My problem is with the last line of code that I entered. As you can see below this is the output that I got.

This customers name is John
This customers age is 20
<__main__.Customer object at 0x7f09f6c494c0> is now buying something

Upvotes: 0

Views: 43

Answers (1)

John Byro
John Byro

Reputation: 734

You need to have __str__ or __repr__ method in class

Add this code

def __str__(self):
    return f'{self.first}_{self.last}_{self.age}_{self.gender}'

It will print something John_Doe_20_Male is now buying something

Upvotes: 2

Related Questions