Hi all,
I am doing the Python project but I am stuck now without knowing the cause of the error. I am my codes are below, can you guys help me identify what went wrong?
Thanks soooo much people!!
----- my code ------
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.email = address
return self.email
def __repr__(self):
return "User {name}, email: {email}, books read: {books}".format(name = self.name, email = self.email, books = len(self.book))
def __eq__(self, other_user):
if self.name == other_user.name and self.email == other_user.email:
return True
return False
def read_book(self, book, rating=None):
self.book.update({book: rating})
return self.book
def get_average_rating(self):
self.rating_count = 0
self.rating_total = 0
for rating in self.book.values():
self.rating_count += 1
self.rating_total = self.rating_total + int(rating)
return self.rating_total / self.rating_count
class Book(object):
def init(self, title, isbn):
self.title = title
self.isbn = isbn
self.ratings = [ ]
def __hash__(self):
return hash((self.title, self.isbn))
def get_title(self):
return self.title
def get_isbn(self):
return self.isbn
def set_isbn(self, new_isbn):
self.isbn = new_isbn
print("This book's ISBN has been updated to {new_ISBN}".format(new_ISBN = self.isbn))
return self.isbn
def add_rating(self, rating):
try:
if 0 <= rating <= 4:
self.ratings.append(rating)
else:
return "Invalid Rating."
except TypeError:
"Invalid Type."
def __eq__(self, other_book):
if self.title == other_book.title and self.isbn == other_book.isbn:
return True
else: return False
def get_average_rating(self):
total_rating = 0
for i in self.ratings:
total_rating += int(i)
return total_rating / len(self.ratings)
class Fiction(Book):
def __init__(self, title, author, isbn):
super().__init__(title, isbn)
self.author = author
def get_author(self):
return self.author
def __repr__(self):
return "{title} by {author}".format(title = self.title, author = self.author)
class Non_Fiction(Book):
def __init__(self, title, isbn, subject, level):
super().__init__(title, isbn)
self.subject = subject
self.level = level
def get_subject(self):
return self.subject
def get_level(self):
return self.level
def __repr__(self):
return "{title}, a {level} manual on {subject}".format(title = self.title, level = self.level, subject = self.subject)
class TomeRater(User,Book):
def __init__(self):
self.users = {"[email protected]" : 0}
self.books = {}
def create_book(self, title, isbn):
return Book(title, isbn)
def create_novel(self, title, author, isbn):
return Fiction(title, author, isbn)
def create_non_fiction(self, title, subject, level, isbn):
return Non_Fiction(title, subject, level, isbn)
def add_user(self, name, email, user_books = None):
if email not in self.users.keys():
user_adding = User(name, email)
self.users.update({user_adding.email:user_adding.name})
print(self.users)
if user_books is not None:
for book in user_books:
self.add_book_to_user(book, email)
def add_book_to_user(self, book, email, rating=None):
if email in self.users.keys():
User.read_book(book, rating)
Book.add_rating(rating)
if book in User.self.books.keys():
return User.self.books(book) + 1
else: User.read_book(book)
print(self.books)
else:
print("No user with email {email}!".format(email = email))
def print_catalog(self):
for book in self.books.keys():
print(book)
def print_users(self):
for user in self.users.values():
print(user)
def most_read_book(self):
return max(self.books, key=self.books.get)
def highest_rated_book(self):
highest_rated = max(rating.get_average_rating() for rating in self.books.keys())
def most_positive_user(self):
most_positive = max(user.get_average_rating() for user in self.users.values())