Function __repr__Final Project - TomRater

Hi guys,
I am working on the last project for python called TomRater.

I need to do the following:

A repr method that returns a string to print out this user

object in a meaningful way. Printing a user named Stephen Hawking, with an email hawking@universe.edu, with 7 books read,

might produce a string like: User Stephen Hawking, email: hawking@universe.edu, books read: 7

but when I print this:

class User(object):
def init(self, name, email,book):
self.name = name
self.email = email
self.book = book

def get_email(self):
    return self.email

def change_email(self, address):
    self.address = address
    if address != self.email:
        str.replace(self.email, self.address)
        print("this user’s email has been updated")



def __repr__(self):
    return 'User(name =%, email=%, book=%s)' %(self.name, self,email, self.book)

Userr = User(marion, baa@kd, 3)
print(Userr)

it doesn’t work. I am not writting the function correctly I guess…

Can someone guide me because I am lost…

Thanks !

Two of those parameters are without an expected type. %s will do the trick.

We can’t tell about all the indentation but indications are that the methods are in the __init__ block.

thank you for your answer, however it still doesn’t work…

class User(object):
    def __init__(self, name, email):
        self.name = name
        self.email = email
        self.book = {}

    def get_email(self):
        return self.email

    def change_email(self, address):
        self.address = address
        if address != self.email:
            str.replace(self.email, self.address)
            print("this user’s email has been updated")


    def __repr__(self):
        return "User(%s, %s, %s)" % (self.name, self.email, self.book)

    def __eq__(self, other_user):
        pass
Userr = User(marion,bala@ht,3)
print(Userr)
str.replace

str in the above is the string upon which the method is operating.

>>> "midtake".replace("d", "s")
'mistake'
>>> 

It follows that we would validate user inputs before assignment.

if address != self.email:
    self.email = address

It appears there is no need for the replace method.

Aside

Your code look to follow Python 3 syntax (at least the print() statement). A little research will find that Python 3 classes no longer need to explicitly inherit from object.

class User:

Also bear in mind that by convention, class instance names are not capitalized.

user = User(..)

Thank you very much for your help !
This has been very useful.

Marion

1 Like