I just completed a video exercise from The Modern Python Bootcamp by Colt Steele (Udemy) - Object Oriented Programming. The exercise uses a class User
that has attributes for first name, last name, and age. There is also a class method birthday()
that wishes the person a happy birthday along with their age.
The issue I have with the birthday method is that it has a static "th"
after the person's age, which doesn't make sense if the person is 1, 61, 52 etc. How can I determine if the number has a 1 or 2 at the end or in the case of 1st the begin of the number? Would I have to create a tuple that I test against age?
class User:
def __init__(self, first, last, age):
self.first = first
self.last = last
self.age = age
def full_name(self):
return f"{self.first} {self.last}"
def initials(self):
return f"{self.first[0]}.{self.last[0]}."
def likes(self, thing):
return f"{self.first} likes {thing}"
def is_senior(self):
return self.age >= 65
def birthday(self):
self.age += 1
return f"Happy {self.age}th, {self.first}"
user1 = User("Joe", "Smith", 21)
user2 = User("Blanca", "Lopez", 20)
print(user2.is_senior())
print(user2.birthday())
print(user1.age)