-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuser.py
More file actions
24 lines (20 loc) · 743 Bytes
/
user.py
File metadata and controls
24 lines (20 loc) · 743 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class User:
def __init__(self, name, user_id):
self.name = name
self.user_id = user_id
self.ratings = []
def add_rating(self, rating):
if not isinstance(rating, (int, float)):
raise ValueError("Rating must be a number.")
if rating < 1 or rating > 5:
raise ValueError("Rating must be between 1 and 5.")
self.ratings.append(rating)
def get_average_rating(self):
if not self.ratings:
return 0.0
return sum(self.ratings)/len(self.ratings)
def __str__ (self):
avg = self.get_average_rating()
if avg is None:
return f"User {self.name} (User ID: {self.user_id}) has no ratings yet."
return f"User {self.name} (User ID: {self.user_id}) has an average rating of {avg:.1f}"