Skip to content

Instantly share code, notes, and snippets.

@ZechCodes
Created October 27, 2020 01:32
Show Gist options
  • Save ZechCodes/8f50c58c5767d5739d6b2d24f8d73d89 to your computer and use it in GitHub Desktop.
Save ZechCodes/8f50c58c5767d5739d6b2d24f8d73d89 to your computer and use it in GitHub Desktop.
Challenge 140 - Older Than Me

Challenge 140 - Older Than Me

Create a Person class which which takes name and age parameters at initialization. Then create a method compare_age which takes a person as a parameter and returns a string with the following format:

{other_person} is {older than / younger than / the same age as} me.

Examples

p1 = Person("Samuel", 24)
p2 = Person("Joel", 36)
p3 = Person("Lily", 24)
p1.compare_age(p2) ➞ "Joel is older than me."

p2.compare_age(p1) ➞ "Samuel is younger than me."

p1.compare_age(p3) ➞ "Lily is the same age as me."
from __future__ import annotations
import unittest
class Person:
def __init__(self, name: str, age: int):
...
def compare_age(self, other_person: Person) -> str:
return ""
p1 = Person("Samuel", 24)
p2 = Person("Joel", 36)
p3 = Person("Lily", 24)
class Test(unittest.TestCase):
def test_1(self):
self.assertEqual("Joel is older than me.", p1.compare_age(p2))
def test_2(self):
self.assertEqual("Lily is the same age as me.", p1.compare_age(p3))
def test_3(self):
self.assertEqual("Samuel is younger than me.", p2.compare_age(p1))
def test_4(self):
self.assertEqual("Lily is younger than me.", p2.compare_age(p3))
def test_5(self):
self.assertEqual("Samuel is the same age as me.", p3.compare_age(p1))
def test_6(self):
self.assertEqual("Joel is older than me.", p3.compare_age(p2))
if __name__ == "__main__":
unittest.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment