From eb5b9e2626ac8e148765dd1188bdc5f275813a41 Mon Sep 17 00:00:00 2001 From: Sara Tahir Date: Tue, 2 Dec 2025 11:47:06 +0000 Subject: [PATCH 01/17] Writing bugs that I saw and the types as comments according to me. --- Sprint5/type.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 Sprint5/type.py diff --git a/Sprint5/type.py b/Sprint5/type.py new file mode 100644 index 00000000..60e7f0ef --- /dev/null +++ b/Sprint5/type.py @@ -0,0 +1,30 @@ +def open_account(balances, name, amount): #dic type str and int + balances[name] = amount + +def sum_balances(accounts): #takes type dic[str, int}returns int + total = 0 + for name, pence in accounts.items(): + print(f"{name} had balance {pence}") + total += pence + return total + +def format_pence_as_string(total_pence): #type int returns type str + if total_pence < 100: + return f"{total_pence}p" + pounds = int(total_pence / 100) + pence = total_pence % 100 + return f"£{pounds}.{pence:02d}" + +balances = { #dic type {str:int} + "Sima": 700, + "Linn": 545, + "Georg": 831, +} + +open_account("Tobi", 9.13) # balances not passed, amount should be an int and not a float/ string +open_account("Olya", "£7.13") + +total_pence = sum_balances(balances) #returntype int +total_string = format_pence_as_str(total_pence) #returntype str and wrong function name + +print(f"The bank accounts total {total_string}") \ No newline at end of file From d937285726ffc4f38323d006722b2d2ed7b9baff Mon Sep 17 00:00:00 2001 From: Sara Tahir Date: Tue, 2 Dec 2025 12:11:13 +0000 Subject: [PATCH 02/17] first step of adding types. and running mypy on terminal --- Sprint5/type.py | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/Sprint5/type.py b/Sprint5/type.py index 60e7f0ef..dc56751f 100644 --- a/Sprint5/type.py +++ b/Sprint5/type.py @@ -1,21 +1,27 @@ -def open_account(balances, name, amount): #dic type str and int + +from typing import Dict +def open_account(balances: Dict[str,int], name:str, amount:int): #dic type str and int + #Adding a new account balances[name] = amount -def sum_balances(accounts): #takes type dic[str, int}returns int - total = 0 +def sum_balances(accounts:Dict[str,int])-> int: #takes type dic[str, int}returns int + total = int = 0 for name, pence in accounts.items(): print(f"{name} had balance {pence}") total += pence return total -def format_pence_as_string(total_pence): #type int returns type str +def format_pence_as_string(total_pence: int)-> str: #type int returns type str + #formatting pence as a string if total_pence < 100: return f"{total_pence}p" - pounds = int(total_pence / 100) + pounds =int(total_pence / 100) + reveal_type(pounds) pence = total_pence % 100 + reveal_type(pence) return f"£{pounds}.{pence:02d}" -balances = { #dic type {str:int} +balances:Dict[str,int] = { #dic type {str:int} "Sima": 700, "Linn": 545, "Georg": 831, @@ -25,6 +31,7 @@ def format_pence_as_string(total_pence): #type int returns type str open_account("Olya", "£7.13") total_pence = sum_balances(balances) #returntype int -total_string = format_pence_as_str(total_pence) #returntype str and wrong function name - +reveal_type(total_pence) +total_string = format_pence_as_str(total_pence) #returntype str and wrong function name +reveal_type(total_string) print(f"The bank accounts total {total_string}") \ No newline at end of file From 7dbbd28b111c233df6dc43c09b4f132e832df297 Mon Sep 17 00:00:00 2001 From: Sara Tahir Date: Tue, 2 Dec 2025 12:20:01 +0000 Subject: [PATCH 03/17] Fixed all bug, also ran mypy with --strict flag to pick every issue. All bugs have been resolved. --- Sprint5/type.py | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/Sprint5/type.py b/Sprint5/type.py index dc56751f..9340cf25 100644 --- a/Sprint5/type.py +++ b/Sprint5/type.py @@ -1,6 +1,6 @@ from typing import Dict -def open_account(balances: Dict[str,int], name:str, amount:int): #dic type str and int +def open_account(balances: Dict[str,int], name:str, amount:int)->None: #dic type str and int--- returns none/figured after running --strict flag with mypy #Adding a new account balances[name] = amount @@ -15,10 +15,8 @@ def format_pence_as_string(total_pence: int)-> str: #type int returns type str #formatting pence as a string if total_pence < 100: return f"{total_pence}p" - pounds =int(total_pence / 100) - reveal_type(pounds) + pounds =(total_pence // 100) #floordivision always returns an int pence = total_pence % 100 - reveal_type(pence) return f"£{pounds}.{pence:02d}" balances:Dict[str,int] = { #dic type {str:int} @@ -27,11 +25,10 @@ def format_pence_as_string(total_pence: int)-> str: #type int returns type str "Georg": 831, } -open_account("Tobi", 9.13) # balances not passed, amount should be an int and not a float/ string -open_account("Olya", "£7.13") +open_account(balances,"Tobi", 913) # correct passing of the argumenst to functions with balances. +open_account(balances,"Olya", 713) total_pence = sum_balances(balances) #returntype int -reveal_type(total_pence) -total_string = format_pence_as_str(total_pence) #returntype str and wrong function name -reveal_type(total_string) +total_string = format_pence_as_string(total_pence) #returntype str and wrong function name + print(f"The bank accounts total {total_string}") \ No newline at end of file From 882b78f7926f7651a709a05fdbe5b097fbe29779 Mon Sep 17 00:00:00 2001 From: Sara Tahir Date: Tue, 2 Dec 2025 12:29:35 +0000 Subject: [PATCH 04/17] read and understood the error --- Sprint5/classes.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 Sprint5/classes.py diff --git a/Sprint5/classes.py b/Sprint5/classes.py new file mode 100644 index 00000000..b7712d03 --- /dev/null +++ b/Sprint5/classes.py @@ -0,0 +1,14 @@ +class Person: + def __init__(self, name: str, age: int, preferred_operating_system: str): + self.name = name + self.age = age + self.preferred_operating_system = preferred_operating_system + +imran = Person("Imran", 22, "Ubuntu") +print(imran.name) +print(imran.address) + +eliza = Person("Eliza", 34, "Arch Linux") +print(eliza.name) +print(eliza.address) +# error said person has no attribute "address" in line 9 and 13 \ No newline at end of file From 6771d14c6df0633af3d9d643371f2a52aa11021e Mon Sep 17 00:00:00 2001 From: Sara Tahir Date: Tue, 2 Dec 2025 12:32:23 +0000 Subject: [PATCH 05/17] No error mesages. mypy infers that person has a property of age --- Sprint5/classes.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/Sprint5/classes.py b/Sprint5/classes.py index b7712d03..8ec5da73 100644 --- a/Sprint5/classes.py +++ b/Sprint5/classes.py @@ -3,12 +3,15 @@ def __init__(self, name: str, age: int, preferred_operating_system: str): self.name = name self.age = age self.preferred_operating_system = preferred_operating_system - imran = Person("Imran", 22, "Ubuntu") print(imran.name) -print(imran.address) +#print(imran.address) eliza = Person("Eliza", 34, "Arch Linux") print(eliza.name) -print(eliza.address) -# error said person has no attribute "address" in line 9 and 13 \ No newline at end of file +#print(eliza.address) + +def is_adult(person: Person) -> bool: + return person.age >= 18 + +print(is_adult(imran)) \ No newline at end of file From 4e2e07e9f887916dcc6ece969362a0b2109a2863 Mon Sep 17 00:00:00 2001 From: Sara Tahir Date: Tue, 2 Dec 2025 12:36:00 +0000 Subject: [PATCH 06/17] It does report an error because cant access an attribute that is not given in dic --- Sprint5/classes.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Sprint5/classes.py b/Sprint5/classes.py index 8ec5da73..6df932c7 100644 --- a/Sprint5/classes.py +++ b/Sprint5/classes.py @@ -14,4 +14,7 @@ def __init__(self, name: str, age: int, preferred_operating_system: str): def is_adult(person: Person) -> bool: return person.age >= 18 -print(is_adult(imran)) \ No newline at end of file +print(is_adult(imran)) + +def works_at(person: Person) -> str: + return person.jobcompany \ No newline at end of file From 77ca2f636251ccf9415ff1a343b7de99fa0b6038 Mon Sep 17 00:00:00 2001 From: Sara Tahir Date: Tue, 2 Dec 2025 14:25:04 +0000 Subject: [PATCH 07/17] Refactor Person class: replace age field with date of birth parameter --- Sprint5/methods.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 Sprint5/methods.py diff --git a/Sprint5/methods.py b/Sprint5/methods.py new file mode 100644 index 00000000..13963c24 --- /dev/null +++ b/Sprint5/methods.py @@ -0,0 +1,24 @@ +from datetime import date +class Person: + def __init__(self, name: str, birth_day: date): + self.name = name + self.birth_day = birth_day + + def age(self)-> int: #returns age in years + today = date.today() + years = today.year - self.birth_day.year + if(today.month, today.day) < (self.birth_day.month, self.birth_day.day): + years -= 1 + return years + + def is_adult(self): + return self.age() >= 18 + + def __str__(self) -> str: + return f"{self.name}, born {self.birth_day}, age {self.age()}" + +p1 = Person("Sara", date(1996, 10, 28)) +print(p1.is_adult()) # True + +p2 = Person("Muhib", date(2018, 6, 22)) +print(p2.is_adult()) # False \ No newline at end of file From 6f001880dd2bb6b9119518dfde6e96e10cf78391 Mon Sep 17 00:00:00 2001 From: Sara Tahir Date: Tue, 2 Dec 2025 21:40:44 +0000 Subject: [PATCH 08/17] incorporating dataclasses in the code --- Sprint5/data-class.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 Sprint5/data-class.py diff --git a/Sprint5/data-class.py b/Sprint5/data-class.py new file mode 100644 index 00000000..24851716 --- /dev/null +++ b/Sprint5/data-class.py @@ -0,0 +1,25 @@ +from datetime import date +from dataclasses import dataclass + +@dataclass(frozen=True) +class Person: + name: str + birth_day: date + preferred_operating_system: str + + def age(self)-> int: #returns age in years + today = date.today() + years = today.year - self.birth_day.year + if(today.month, today.day) < (self.birth_day.month, self.birth_day.day): + years -= 1 + return years + + def is_adult(self): + return self.age() >= 18 + + +p1 = Person("Sara", date(1996, 10, 28), "macOS") +print(p1.is_adult()) # True + +p2 = Person("Muhib", date(2018, 6, 22), "Linux") +print(p2.is_adult()) # False \ No newline at end of file From 2dc539b3252a0a369eded2c386291ac3df985711 Mon Sep 17 00:00:00 2001 From: Sara Tahir Date: Tue, 2 Dec 2025 21:42:50 +0000 Subject: [PATCH 09/17] testing for value based equality with dataclass. --- Sprint5/data-class.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Sprint5/data-class.py b/Sprint5/data-class.py index 24851716..0d125ff7 100644 --- a/Sprint5/data-class.py +++ b/Sprint5/data-class.py @@ -19,7 +19,7 @@ def is_adult(self): p1 = Person("Sara", date(1996, 10, 28), "macOS") -print(p1.is_adult()) # True +print(p1) -p2 = Person("Muhib", date(2018, 6, 22), "Linux") -print(p2.is_adult()) # False \ No newline at end of file +p2 = Person("Sara", date(1996, 10, 28), "macOS") +print(p1 == p2) \ No newline at end of file From 2eb49e948572ae0d2ba4e7add3e0ddc459abb772 Mon Sep 17 00:00:00 2001 From: Sara Tahir Date: Tue, 2 Dec 2025 22:03:21 +0000 Subject: [PATCH 10/17] adding generic type to children:list and adding age for the children. --- Sprint5/generics.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 Sprint5/generics.py diff --git a/Sprint5/generics.py b/Sprint5/generics.py new file mode 100644 index 00000000..71fd4c46 --- /dev/null +++ b/Sprint5/generics.py @@ -0,0 +1,21 @@ +from dataclasses import dataclass + +@dataclass(frozen=True) +class Person: + name: str + age : int + children: list["Person"] #nside the Person class- the Person-type doesnt exist, so we add "" to person, + + + +Muhib = Person(name="Muhib",age= 7, children=[]) +Muiz = Person(name="Muiz",age= 4,children=[]) + +Sara = Person(name="Sara",age= 31, children=[Muhib, Muiz]) + +def print_family_tree(person: Person) -> None: + print(person.name) + for child in person.children: + print(f" {child.name} ({child.age})") + +print_family_tree(Sara) \ No newline at end of file From a5faf8be481cd3af28d8d1e10f3c880b5ac0cac7 Mon Sep 17 00:00:00 2001 From: Sara Tahir Date: Wed, 3 Dec 2025 14:55:58 +0000 Subject: [PATCH 11/17] Refactor Person to use preferred_operating_systems as List[str] --- Sprint5/refactor.py | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 Sprint5/refactor.py diff --git a/Sprint5/refactor.py b/Sprint5/refactor.py new file mode 100644 index 00000000..c2d34ef5 --- /dev/null +++ b/Sprint5/refactor.py @@ -0,0 +1,42 @@ +from dataclasses import dataclass +from typing import List + +@dataclass(frozen=True) +class Person: + name: str + age: int + preferred_operating_systems: list[str] + + +@dataclass(frozen=True) +class Laptop: + id: int + manufacturer: str + model: str + screen_size_in_inches: float + operating_system: str + + +def find_possible_laptops(laptops: List[Laptop], person: Person) -> List[Laptop]: + possible_laptops = [] + for laptop in laptops: + if laptop.operating_system in person.preferred_operating_systems: + possible_laptops.append(laptop) + return possible_laptops + + +people = [ + Person(name="Imran", age=22, preferred_operating_systems=["Ubuntu"]), + Person(name="Eliza", age=34, preferred_operating_systems=["Arch Linux"]), +] + +laptops = [ + Laptop(id=1, manufacturer="Dell", model="XPS", screen_size_in_inches=13, operating_system="Arch Linux"), + Laptop(id=2, manufacturer="Dell", model="XPS", screen_size_in_inches=15, operating_system="Ubuntu"), + Laptop(id=3, manufacturer="Dell", model="XPS", screen_size_in_inches=15, operating_system="ubuntu"), + Laptop(id=4, manufacturer="Apple", model="macBook", screen_size_in_inches=13, operating_system="macOS"), +] + +for person in people: + possible_laptops = find_possible_laptops(laptops, person) + print(f"Possible laptops for {person.name}: {possible_laptops}") \ No newline at end of file From cfa95d627c53b01e9bf48953c5907c77d839f445 Mon Sep 17 00:00:00 2001 From: Sara Tahir Date: Thu, 4 Dec 2025 13:33:28 +0000 Subject: [PATCH 12/17] Add laptop library with enum validation and OS availability check --- Sprint5/e-num.py | 90 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 Sprint5/e-num.py diff --git a/Sprint5/e-num.py b/Sprint5/e-num.py new file mode 100644 index 00000000..ed53f211 --- /dev/null +++ b/Sprint5/e-num.py @@ -0,0 +1,90 @@ +from dataclasses import dataclass +from enum import Enum +from typing import List +import sys + + +class OperatingSystem(Enum): + MACOS = "macOS" + ARCH = "Arch Linux" + UBUNTU = "Ubuntu" + +@dataclass(frozen=True) +class Person: + name: str + age: int + preferred_os: OperatingSystem + + +@dataclass(frozen=True) +class Laptop: + id: int + manufacturer: str + model: str + screen_size_in_inches: float + operating_system: OperatingSystem + +#library of laptops +laptops = [ + Laptop(id=1, manufacturer="Dell", model="XPS", screen_size_in_inches=13, operating_system=OperatingSystem.ARCH), + Laptop(id=2, manufacturer="Dell", model="XPS", screen_size_in_inches=15, operating_system=OperatingSystem.UBUNTU), + Laptop(id=3, manufacturer="Dell", model="XPS", screen_size_in_inches=15, operating_system=OperatingSystem.UBUNTU), + Laptop(id=4, manufacturer="Apple", model="macBook", screen_size_in_inches=13, operating_system=OperatingSystem.MACOS), +] + +def find_possible_laptops(laptops: List[Laptop], person: Person) -> List[Laptop]: + possible_laptops = [] + for laptop in laptops: + if laptop.operating_system == person.preferred_os: + possible_laptops.append(laptop) + return possible_laptops # returns an array of possible laptops for the person + + #accept userinput +name = input("Enter your name: ").strip() +age_str = input("Enter your age: ").strip() #input always returns a str, we need to convert this to an integer +os_str = input("Enter your preferred operating system: ").strip() +# Validate age +try: + age = int(age_str) +except ValueError: + print("Error: Age must be a number.", file= sys.stderr) + sys.exit(1) + +# Validate OS +try: + preferred_os = OperatingSystem(os_str) +except ValueError: + print("Error: Invalid operating system.", file=sys.stderr) + sys.exit(1) + +#Create a person after validation +person = Person(name = name, age = age, preferred_os =preferred_os) +#finding laptops for this person +possible = find_possible_laptops(laptops, person) +print(f"The laptop library has {len(possible)} with {preferred_os.value}.") + +# Start with an empty dictionary +os_counts: dict[OperatingSystem, int] = {} + +for laptop in laptops: + os = laptop.operating_system + # If we've seen this OS before, add 1, otherwise start at 1 + if os in os_counts: + os_counts[os] += 1 + else: + os_counts[os] = 1 + +best_os = None +best_count = -1 + +# Loop through each (key, value) pair +for pair in os_counts.items(): + os = pair[0] # the key (operating system) + count = pair[1] # the value (number of laptops) + + # Check if this count is bigger than the best so far + if count > best_count: + best_os = os + best_count = count + +print("Best OS:", best_os, "with", best_count, "laptops") \ No newline at end of file From 7f31e925f9a08d50b70aadf1bde046f6cc6fca9d Mon Sep 17 00:00:00 2001 From: Sara Tahir Date: Thu, 4 Dec 2025 21:48:35 +0000 Subject: [PATCH 13/17] Prediction is correct -> childclass methods cannot be inherited by superclass --- Sprint5/lastexer.py | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 Sprint5/lastexer.py diff --git a/Sprint5/lastexer.py b/Sprint5/lastexer.py new file mode 100644 index 00000000..60c7c9bd --- /dev/null +++ b/Sprint5/lastexer.py @@ -0,0 +1,37 @@ +class Parent: + def __init__(self, first_name: str, last_name: str): + self.first_name = first_name + self.last_name = last_name + + def get_name(self) -> str: # get name is a method of class parent + return f"{self.first_name} {self.last_name}" + + +class Child(Parent): #childclass of parent + def __init__(self, first_name: str, last_name: str): + super().__init__(first_name, last_name) #superclass constructor intialised in childclass + self.previous_last_names = [] #intialised a new attribute + + def change_last_name(self, last_name) -> None: + self.previous_last_names.append(self.last_name) #append previous lastname to the previous_last_names[] + self.last_name = last_name #set the new one as last_name + + def get_full_name(self) -> str: + suffix = "" + if len(self.previous_last_names) > 0: #if last_name has been changes, it adds the previous last name as a suffix to the new one + suffix = f" (née {self.previous_last_names[0]})" + return f"{self.first_name} {self.last_name}{suffix}" + +person1 = Child("Elizaveta", "Alekseeva") +print(person1.get_name()) +print(person1.get_full_name()) +person1.change_last_name("Tyurina") +print(person1.get_name()) +print(person1.get_full_name()) + +person2 = Parent("Elizaveta", "Alekseeva") +print(person2.get_name()) +print(person2.get_full_name()) #childclass method +person2.change_last_name("Tyurina") #childclass method +print(person2.get_name()) +print(person2.get_full_name()) \ No newline at end of file From 842c004712813755343a0dcdc889fced6f30cda5 Mon Sep 17 00:00:00 2001 From: Sara Tahir Date: Thu, 4 Dec 2025 21:58:08 +0000 Subject: [PATCH 14/17] Removed the error causing lines --- Sprint5/lastexer.py | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/Sprint5/lastexer.py b/Sprint5/lastexer.py index 60c7c9bd..2c3e6bb3 100644 --- a/Sprint5/lastexer.py +++ b/Sprint5/lastexer.py @@ -1,24 +1,31 @@ class Parent: + """Represents a person with a first and last name.""" def __init__(self, first_name: str, last_name: str): self.first_name = first_name self.last_name = last_name - def get_name(self) -> str: # get name is a method of class parent + def get_name(self) -> str: + """Return the full name as 'First Last'.""" return f"{self.first_name} {self.last_name}" -class Child(Parent): #childclass of parent +class Child(Parent): + """Represents a person who can change last names, tracking previous ones.""" def __init__(self, first_name: str, last_name: str): - super().__init__(first_name, last_name) #superclass constructor intialised in childclass - self.previous_last_names = [] #intialised a new attribute + """Initialize a Child with a first and last name, plus a list of previous last names.""" + super().__init__(first_name, last_name) + self.previous_last_names: list[str] = [] def change_last_name(self, last_name) -> None: - self.previous_last_names.append(self.last_name) #append previous lastname to the previous_last_names[] - self.last_name = last_name #set the new one as last_name + """Change the last name and record the previous one.""" + self.previous_last_names.append(self.last_name) + self.last_name = last_name def get_full_name(self) -> str: + """Return the full name, with suffix showing original last name if changed.""" + suffix = "" suffix = "" - if len(self.previous_last_names) > 0: #if last_name has been changes, it adds the previous last name as a suffix to the new one + if len(self.previous_last_names) > 0: suffix = f" (née {self.previous_last_names[0]})" return f"{self.first_name} {self.last_name}{suffix}" @@ -31,7 +38,3 @@ def get_full_name(self) -> str: person2 = Parent("Elizaveta", "Alekseeva") print(person2.get_name()) -print(person2.get_full_name()) #childclass method -person2.change_last_name("Tyurina") #childclass method -print(person2.get_name()) -print(person2.get_full_name()) \ No newline at end of file From dfc29a86ac60e57cca9d84dd301f0078d3781f41 Mon Sep 17 00:00:00 2001 From: Sara Tahir Date: Thu, 4 Dec 2025 22:12:53 +0000 Subject: [PATCH 15/17] Adding a print statement to tell about greater chnaces of getting a laptop based on os --- Sprint5/e-num.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Sprint5/e-num.py b/Sprint5/e-num.py index ed53f211..19bccf8d 100644 --- a/Sprint5/e-num.py +++ b/Sprint5/e-num.py @@ -87,4 +87,7 @@ def find_possible_laptops(laptops: List[Laptop], person: Person) -> List[Laptop] best_os = os best_count = count -print("Best OS:", best_os, "with", best_count, "laptops") \ No newline at end of file +print("Best OS:", best_os, "with", best_count, "laptops") +if best_os != preferred_os: + print(f"If you’re willing to accept {best_os.value}, " + f"you’re more likely to get a laptop since there are {best_count} available.") \ No newline at end of file From af120e7f6b467b10095552537b76f5fb51dd9c4e Mon Sep 17 00:00:00 2001 From: Sara Tahir Date: Fri, 5 Dec 2025 11:34:15 +0000 Subject: [PATCH 16/17] Solving 1- double exercises --- Sprint5/double.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 Sprint5/double.py diff --git a/Sprint5/double.py b/Sprint5/double.py new file mode 100644 index 00000000..f6f5ec6b --- /dev/null +++ b/Sprint5/double.py @@ -0,0 +1,21 @@ +def double(n): + return n*2 +num = double("22") +print(num) + +#my prediction is that it would return 2222. As python will treat 22 as a string being a strongly typed language. +#if it was javascript * operator would have forced numeric converion and 22 string would have become 22 int and 44 would +#have been returned +# when I run the file it returns 2222 as expected. + +# 2nd exercise- original function : +def double(number): + return number * 3 + +print(double(10)) # returns "30" + +#so we either change the name of the function to triple or multiply number with 2 rather than 3 +def triple(number): + return number * 3 + +print(triple(10)) # returns "30" \ No newline at end of file From b5b4a6fd6d0b6fb607a0ce669760263449daba72 Mon Sep 17 00:00:00 2001 From: Sara Tahir Date: Mon, 19 Jan 2026 22:14:57 +0000 Subject: [PATCH 17/17] making necessary changes based on code review --- Sprint5/classes.py | 2 -- Sprint5/generics.py | 6 +++--- Sprint5/type.py | 7 +++++-- 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/Sprint5/classes.py b/Sprint5/classes.py index 6df932c7..3e662ac5 100644 --- a/Sprint5/classes.py +++ b/Sprint5/classes.py @@ -16,5 +16,3 @@ def is_adult(person: Person) -> bool: print(is_adult(imran)) -def works_at(person: Person) -> str: - return person.jobcompany \ No newline at end of file diff --git a/Sprint5/generics.py b/Sprint5/generics.py index 71fd4c46..1ef35c8c 100644 --- a/Sprint5/generics.py +++ b/Sprint5/generics.py @@ -13,9 +13,9 @@ class Person: Sara = Person(name="Sara",age= 31, children=[Muhib, Muiz]) -def print_family_tree(person: Person) -> None: - print(person.name) +def print_family_tree(person: Person, indent: int = 0) -> None: + print(" " * indent + f"{person.name} ({person.age})") #indent(int)represents how many spaces to put before the person’s name when printing. for child in person.children: - print(f" {child.name} ({child.age})") + print_family_tree(child, indent + 2) print_family_tree(Sara) \ No newline at end of file diff --git a/Sprint5/type.py b/Sprint5/type.py index 9340cf25..842dce46 100644 --- a/Sprint5/type.py +++ b/Sprint5/type.py @@ -13,11 +13,14 @@ def sum_balances(accounts:Dict[str,int])-> int: #takes type dic[str, int}returns def format_pence_as_string(total_pence: int)-> str: #type int returns type str #formatting pence as a string + sign = "-" if total_pence < 0 else "" + total_pence = abs(total_pence) + if total_pence < 100: - return f"{total_pence}p" + return f"{sign}{total_pence}p" pounds =(total_pence // 100) #floordivision always returns an int pence = total_pence % 100 - return f"£{pounds}.{pence:02d}" + return f"{sign}£{pounds}.{pence:02d}" balances:Dict[str,int] = { #dic type {str:int} "Sima": 700,