From 2d6dd408a4c6f361ddaa4e674ae998ad6bd320d3 Mon Sep 17 00:00:00 2001 From: pathywang Date: Thu, 23 Jul 2026 23:06:18 +0100 Subject: [PATCH 01/14] done --- .gitignore | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/.gitignore b/.gitignore index c4234ab74..5bb64fa54 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,7 @@ + node_modules testoutput.txt + +.venv/ +__pycache__/ +*.pyc From 20478fbec2a940067c1625e964e1f629aa24ec6d Mon Sep 17 00:00:00 2001 From: pathywang Date: Thu, 30 Jul 2026 10:02:27 +0100 Subject: [PATCH 02/14] update exercises --- Spring 5 exercises/checktype.py | 33 +++++++++++++++++++++++++++++++ Spring 5 exercises/type_exaple.py | 17 ++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 Spring 5 exercises/checktype.py create mode 100644 Spring 5 exercises/type_exaple.py diff --git a/Spring 5 exercises/checktype.py b/Spring 5 exercises/checktype.py new file mode 100644 index 000000000..26d42da0f --- /dev/null +++ b/Spring 5 exercises/checktype.py @@ -0,0 +1,33 @@ +def open_account(balances, name, amount): + balances[name] = amount + +def sum_balances(accounts): + 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): + if total_pence < 100: + return f"{total_pence}p" + pounds = int(total_pence / 100) + pence = total_pence % 100 + return f"£{pounds}.{pence:02d}" + +balances = { + "Sima": 700, + "Linn": 545, + "Georg": 831, +} + +open_account(balances,"Tobi", 913) +open_account(balances,"Olya", 713) + +total_pence = sum_balances(balances) +total_string = format_pence_as_string(total_pence) + +print(f"The bank accounts total {total_string}") + + # i amended three errors from original code from prep, line 28 function name is format_pence-as_string rather than format_pence_as_str, line 24 ad 25, supposed + # three arguments instead of 2, so i add balances when we call open_account and also call open_account amount should be integer rather than string or decimal \ No newline at end of file diff --git a/Spring 5 exercises/type_exaple.py b/Spring 5 exercises/type_exaple.py new file mode 100644 index 000000000..c166df9ab --- /dev/null +++ b/Spring 5 exercises/type_exaple.py @@ -0,0 +1,17 @@ +def double(number): + return number * 3 + +print(double(10)) + +# Because double conflicts with multipying 3 so we can change function name to 'triple' or '*2' + +def triple(number): + return number * 3 + +print(triple(10)) + + +def double(number): + return number * 2 + +print(double(10)) From 8a8c9ab5af39d59ed98a3f6f7d708905994b5bde Mon Sep 17 00:00:00 2001 From: pathywang Date: Thu, 30 Jul 2026 10:42:30 +0100 Subject: [PATCH 03/14] code error --- Spring 5 exercises/class.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 Spring 5 exercises/class.py diff --git a/Spring 5 exercises/class.py b/Spring 5 exercises/class.py new file mode 100644 index 000000000..aaf7f6de1 --- /dev/null +++ b/Spring 5 exercises/class.py @@ -0,0 +1,21 @@ +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) + +eliza = Person("Eliza", 34, "Arch Linux") +print(eliza.name) + +def is_adult(person: Person) -> bool: + return person.age >= 18 + +print(is_adult(imran)) + +def homeadress(person :Person): + return person.address + +print(homeadress(eliza)) \ No newline at end of file From 998fc6c3da478f4ba4a0b496f8a1b637806317bf Mon Sep 17 00:00:00 2001 From: pathywang Date: Thu, 30 Jul 2026 10:46:20 +0100 Subject: [PATCH 04/14] add address --- Spring 5 exercises/class.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/Spring 5 exercises/class.py b/Spring 5 exercises/class.py index aaf7f6de1..ff384ce93 100644 --- a/Spring 5 exercises/class.py +++ b/Spring 5 exercises/class.py @@ -1,13 +1,14 @@ class Person: - def __init__(self, name: str, age: int, preferred_operating_system: str): + def __init__(self, name: str, age: int, preferred_operating_system: str,address:str): self.name = name self.age = age self.preferred_operating_system = preferred_operating_system + self.address=address -imran = Person("Imran", 22, "Ubuntu") +imran = Person("Imran", 22, "Ubuntu", "23 main raod") print(imran.name) -eliza = Person("Eliza", 34, "Arch Linux") +eliza = Person("Eliza", 34, "Arch Linux","32 divert way") print(eliza.name) def is_adult(person: Person) -> bool: @@ -15,7 +16,7 @@ def is_adult(person: Person) -> bool: print(is_adult(imran)) -def homeadress(person :Person): +def home_address(person :Person): return person.address -print(homeadress(eliza)) \ No newline at end of file +print(home_address(eliza)) From 2caed1ebf3ae5b1ff16c817291f92c8fc7226997 Mon Sep 17 00:00:00 2001 From: pathywang Date: Thu, 30 Jul 2026 12:37:23 +0100 Subject: [PATCH 05/14] compete methods --- Spring 5 exercises/methods.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 Spring 5 exercises/methods.py diff --git a/Spring 5 exercises/methods.py b/Spring 5 exercises/methods.py new file mode 100644 index 000000000..e2013be6d --- /dev/null +++ b/Spring 5 exercises/methods.py @@ -0,0 +1,29 @@ + # Think of the advantages of using methods instead of free functions: + # Better organization, More readable code, Easier maintenance,Better editor support (autocomplete),Code reuse across all instances of the class + # Ease of documentation: Related data and behavior are grouped together, making the class easier to understand. + # Encapsulation: The implementation can change without affecting the rest of the program, as long as the method's interface stays the same. + +from datetime import date + + +class Person: + def __init__(self, name: str, date_of_birth:date, preferred_operating_system: str): + self.name = name + self.date_of_birth = date_of_birth + self.preferred_operating_system = preferred_operating_system + + def is_adult(self): + today= date.today() + + age=today.year- self.date_of_birth .year + + if(today.month, today.day) < (self.date_of_birth.month, self.date_of_birth.day): + age-=1 + + return age>=18 + +imaran = Person("Imaran",date(2003,1,5), "Ubuntun") +eliza = Person("Eliza",date(2012,11,25), "Ubuntun") + +print(imaran.is_adult()) +print(eliza.is_adult()) \ No newline at end of file From 1be1836ac88ca83106ad7930a918e78fcd6212c6 Mon Sep 17 00:00:00 2001 From: pathywang Date: Thu, 30 Jul 2026 14:39:16 +0100 Subject: [PATCH 06/14] complete dataclass --- Spring 5 exercises/dataclass.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 Spring 5 exercises/dataclass.py diff --git a/Spring 5 exercises/dataclass.py b/Spring 5 exercises/dataclass.py new file mode 100644 index 000000000..44e265958 --- /dev/null +++ b/Spring 5 exercises/dataclass.py @@ -0,0 +1,28 @@ +from dataclasses import dataclass +from datetime import date + +@dataclass +class Person: + name : str + date_of_birth: date + preferred_operating_system: str + + def is_adult(self) -> bool : + today = date.today() + age = today.year - self.date_of_birth.year + + if (today.month, today.day) < (self.date_of_birth.month, self.date_of_birth.day): + age-=1 + + return age>=18 + +imaran= Person("Imaran", date(2011,11,23), "Ubuntun") +imara2= Person("Imaran", date(2011,11,23), "Ubuntun") + +print(imaran) +print(imara2) +print(imaran==imara2) +print(imaran.is_adult()) + + + From 5728b92903c05cde5ad48cdb3d44ac9b654952ec Mon Sep 17 00:00:00 2001 From: pathywang Date: Thu, 30 Jul 2026 15:46:01 +0100 Subject: [PATCH 07/14] complete generics --- Spring 5 exercises/generics.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 Spring 5 exercises/generics.py diff --git a/Spring 5 exercises/generics.py b/Spring 5 exercises/generics.py new file mode 100644 index 000000000..0c5f4be20 --- /dev/null +++ b/Spring 5 exercises/generics.py @@ -0,0 +1,20 @@ +from dataclasses import dataclass +from typing import List + +@dataclass(frozen=True) +class Person: + name: str + age : int + children: List["Person"] + +fatma = Person(name="Fatma", age=5, children=[]) +aisha = Person(name="Aisha", age= 1, children=[]) + +imran = Person(name="Imran",age = 32, children=[fatma, aisha]) + +def print_family_tree(person: Person) -> None: + print(person.name) + for child in person.children: + print(f"- {child.name} ({child.age})") + +print_family_tree(imran) \ No newline at end of file From 24c52649b024b226d55b3f32c72a1bdc69d39a80 Mon Sep 17 00:00:00 2001 From: pathywang Date: Fri, 31 Jul 2026 05:56:38 +0100 Subject: [PATCH 08/14] complete refactor --- Spring 5 exercises/refactor.py | 42 ++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 Spring 5 exercises/refactor.py diff --git a/Spring 5 exercises/refactor.py b/Spring 5 exercises/refactor.py new file mode 100644 index 000000000..c3798bb5e --- /dev/null +++ b/Spring 5 exercises/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_system: str + + +@dataclass(frozen=True) +class Laptop: + id: int + manufacturer: str + model: str + screen_size_in_inches: float + operating_system: List [str] + + +def find_possible_laptops(laptops: List[Laptop], person: Person) -> List[Laptop]: + possible_laptops = [] + for laptop in laptops: + if laptop.operating_system == person.preferred_operating_system: + possible_laptops.append(laptop) + return possible_laptops + + +people = [ + Person(name="Imran", age=22, preferred_operating_system="Ubuntu"), + Person(name="Eliza", age=34, preferred_operating_system="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 3fee2a3f6ec60fff30bb4a604ea78fdf7f41e9a9 Mon Sep 17 00:00:00 2001 From: pathywang Date: Fri, 31 Jul 2026 07:34:29 +0100 Subject: [PATCH 09/14] complete enum --- Spring 5 exercises/enum.py | 89 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 Spring 5 exercises/enum.py diff --git a/Spring 5 exercises/enum.py b/Spring 5 exercises/enum.py new file mode 100644 index 000000000..cbe61103a --- /dev/null +++ b/Spring 5 exercises/enum.py @@ -0,0 +1,89 @@ +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_operating_system: OperatingSystem + + +@dataclass(frozen=True) +class Laptop: + id: int + manufacturer: str + model: str + screen_size_in_inches: float + operating_system: OperatingSystem + + +laptops = [ + Laptop(1, "Dell", "XPS", 13, OperatingSystem.ARCH), + Laptop(2, "Dell", "XPS", 15, OperatingSystem.UBUNTU), + Laptop(3, "Dell", "XPS", 15, OperatingSystem.UBUNTU), + Laptop(4, "Apple", "MacBook", 13, OperatingSystem.MACOS), +] + + +# Read name +name = input("Enter your name: ") + +# Read and convert age +try: + age = int(input("Enter your age: ")) +except ValueError: + print("Error: age must be a whole number.", file=sys.stderr) + sys.exit(1) + +# Read and convert operating system +print("Choose an operating system:") +print("- Ubuntu") +print("- Arch Linux") +print("- macOS") + +os_input = input("Preferred operating system: ") + +try: + preferred_os = OperatingSystem(os_input) +except ValueError: + print("Error: invalid operating system.", file=sys.stderr) + sys.exit(1) + +# Create the person +person = Person(name, age, preferred_os) + +# Find matching laptops +matching = [ + laptop + for laptop in laptops + if laptop.operating_system == person.preferred_operating_system +] + +print( + f"\nThe library has {len(matching)} laptop(s) running " + f"{person.preferred_operating_system.value}." +) + +# Count laptops for each operating system +counts = {} + +for laptop in laptops: + os = laptop.operating_system + counts[os] = counts.get(os, 0) + 1 + +best_os = max(counts, key=counts.get) + +if best_os != person.preferred_operating_system: + print( + f"If you're willing to accept {best_os.value}, " + f"there are {counts[best_os]} laptops available." + ) \ No newline at end of file From 81495572ba022b7526cc781e02ba2b527a8680a4 Mon Sep 17 00:00:00 2001 From: pathywang Date: Fri, 31 Jul 2026 07:58:38 +0100 Subject: [PATCH 10/14] complete inheritance --- Spring 5 exercises/inheritance.py | 36 ++++++++++++++++++++ Spring 5 exercises/{enum.py => operation.py} | 0 2 files changed, 36 insertions(+) create mode 100644 Spring 5 exercises/inheritance.py rename Spring 5 exercises/{enum.py => operation.py} (100%) diff --git a/Spring 5 exercises/inheritance.py b/Spring 5 exercises/inheritance.py new file mode 100644 index 000000000..9b70e7967 --- /dev/null +++ b/Spring 5 exercises/inheritance.py @@ -0,0 +1,36 @@ +class Parent: + def __init__(self, first_name: str, last_name: str): + self.first_name = first_name + self.last_name = last_name + + def get_full_name(self) -> str: + return f"{self.first_name} {self.last_name}" + + def change_last_name(self, last_name: str) -> None: + self.last_name = last_name + + +class Child(Parent): + def __init__(self, first_name: str, last_name: str): + super().__init__(first_name, last_name) + self.previous_last_names = [] + + def change_last_name(self, last_name) -> None: + self.previous_last_names.append(self.last_name) + self.last_name = last_name + + def get_full_name(self) -> str: + suffix = "" + 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}" + +person1 = Child("Elizaveta", "Alekseeva") +print(person1.get_full_name()) +person1.change_last_name("Tyurina") +print(person1.get_full_name()) + +person2 = Parent("Elizaveta", "Alekseeva") +print(person2.get_full_name()) +person2.change_last_name("Tyurina") +print(person2.get_full_name()) \ No newline at end of file diff --git a/Spring 5 exercises/enum.py b/Spring 5 exercises/operation.py similarity index 100% rename from Spring 5 exercises/enum.py rename to Spring 5 exercises/operation.py From 74bb1c7705e6747697fca9abf1d761a27569c6fe Mon Sep 17 00:00:00 2001 From: pathywang Date: Mon, 17 Aug 2026 08:26:26 +0100 Subject: [PATCH 11/14] correction spelling --- Spring 5 exercises/checktype.py | 6 +++--- Spring 5 exercises/class.py | 6 +++--- Spring 5 exercises/dataclass.py | 12 ++++++------ Spring 5 exercises/generics.py | 6 +++--- Spring 5 exercises/inheritance.py | 8 ++++---- Spring 5 exercises/methods.py | 6 +++--- Spring 5 exercises/refactor.py | 2 +- Spring 5 exercises/type_exaple.py | 2 +- 8 files changed, 24 insertions(+), 24 deletions(-) diff --git a/Spring 5 exercises/checktype.py b/Spring 5 exercises/checktype.py index 26d42da0f..de8a98795 100644 --- a/Spring 5 exercises/checktype.py +++ b/Spring 5 exercises/checktype.py @@ -18,11 +18,11 @@ def format_pence_as_string(total_pence): balances = { "Sima": 700, "Linn": 545, - "Georg": 831, + "George": 831, } -open_account(balances,"Tobi", 913) -open_account(balances,"Olya", 713) +open_account(balances,"Toby", 913) +open_account(balances,"Olive", 713) total_pence = sum_balances(balances) total_string = format_pence_as_string(total_pence) diff --git a/Spring 5 exercises/class.py b/Spring 5 exercises/class.py index ff384ce93..168e5f38b 100644 --- a/Spring 5 exercises/class.py +++ b/Spring 5 exercises/class.py @@ -5,8 +5,8 @@ def __init__(self, name: str, age: int, preferred_operating_system: str,address: self.preferred_operating_system = preferred_operating_system self.address=address -imran = Person("Imran", 22, "Ubuntu", "23 main raod") -print(imran.name) +amy = Person("Amy", 22, "Ubuntu", "23 main road") +print(amy.name) eliza = Person("Eliza", 34, "Arch Linux","32 divert way") print(eliza.name) @@ -14,7 +14,7 @@ def __init__(self, name: str, age: int, preferred_operating_system: str,address: def is_adult(person: Person) -> bool: return person.age >= 18 -print(is_adult(imran)) +print(is_adult(amy)) def home_address(person :Person): return person.address diff --git a/Spring 5 exercises/dataclass.py b/Spring 5 exercises/dataclass.py index 44e265958..436f710c1 100644 --- a/Spring 5 exercises/dataclass.py +++ b/Spring 5 exercises/dataclass.py @@ -16,13 +16,13 @@ def is_adult(self) -> bool : return age>=18 -imaran= Person("Imaran", date(2011,11,23), "Ubuntun") -imara2= Person("Imaran", date(2011,11,23), "Ubuntun") +amy= Person("Amy", date(2011,11,23), "Ubuntu") +amy2= Person("Amy", date(2011,11,23), "Ubuntu") -print(imaran) -print(imara2) -print(imaran==imara2) -print(imaran.is_adult()) +print(amy) +print(amy2) +print(amy==amy2) +print(amy.is_adult()) diff --git a/Spring 5 exercises/generics.py b/Spring 5 exercises/generics.py index 0c5f4be20..e7f4dbaa0 100644 --- a/Spring 5 exercises/generics.py +++ b/Spring 5 exercises/generics.py @@ -7,14 +7,14 @@ class Person: age : int children: List["Person"] -fatma = Person(name="Fatma", age=5, children=[]) +france = Person(name="France", age=5, children=[]) aisha = Person(name="Aisha", age= 1, children=[]) -imran = Person(name="Imran",age = 32, children=[fatma, aisha]) +amy = Person(name="Amy",age = 32, children=[france, aisha]) def print_family_tree(person: Person) -> None: print(person.name) for child in person.children: print(f"- {child.name} ({child.age})") -print_family_tree(imran) \ No newline at end of file +print_family_tree(amy) \ No newline at end of file diff --git a/Spring 5 exercises/inheritance.py b/Spring 5 exercises/inheritance.py index 9b70e7967..7281dd6b0 100644 --- a/Spring 5 exercises/inheritance.py +++ b/Spring 5 exercises/inheritance.py @@ -25,12 +25,12 @@ def get_full_name(self) -> str: suffix = f" (née {self.previous_last_names[0]})" return f"{self.first_name} {self.last_name}{suffix}" -person1 = Child("Elizaveta", "Alekseeva") +person1 = Child("Ella", "Alice") print(person1.get_full_name()) -person1.change_last_name("Tyurina") +person1.change_last_name("Tina") print(person1.get_full_name()) -person2 = Parent("Elizaveta", "Alekseeva") +person2 = Parent("Ella", "Alice") print(person2.get_full_name()) -person2.change_last_name("Tyurina") +person2.change_last_name("Tina") print(person2.get_full_name()) \ No newline at end of file diff --git a/Spring 5 exercises/methods.py b/Spring 5 exercises/methods.py index e2013be6d..2df0a0bb7 100644 --- a/Spring 5 exercises/methods.py +++ b/Spring 5 exercises/methods.py @@ -22,8 +22,8 @@ def is_adult(self): return age>=18 -imaran = Person("Imaran",date(2003,1,5), "Ubuntun") -eliza = Person("Eliza",date(2012,11,25), "Ubuntun") +amy = Person("Amy",date(2003,1,5), "Ubuntu") +eliza = Person("Eliza",date(2012,11,25), "Ubuntu") -print(imaran.is_adult()) +print(amy.is_adult()) print(eliza.is_adult()) \ No newline at end of file diff --git a/Spring 5 exercises/refactor.py b/Spring 5 exercises/refactor.py index c3798bb5e..90e734736 100644 --- a/Spring 5 exercises/refactor.py +++ b/Spring 5 exercises/refactor.py @@ -26,7 +26,7 @@ def find_possible_laptops(laptops: List[Laptop], person: Person) -> List[Laptop] people = [ - Person(name="Imran", age=22, preferred_operating_system="Ubuntu"), + Person(name="Amy", age=22, preferred_operating_system="Ubuntu"), Person(name="Eliza", age=34, preferred_operating_system="Arch Linux"), ] diff --git a/Spring 5 exercises/type_exaple.py b/Spring 5 exercises/type_exaple.py index c166df9ab..90c7a6977 100644 --- a/Spring 5 exercises/type_exaple.py +++ b/Spring 5 exercises/type_exaple.py @@ -3,7 +3,7 @@ def double(number): print(double(10)) -# Because double conflicts with multipying 3 so we can change function name to 'triple' or '*2' +# Because double conflicts with multiplying 3 so we can change function name to 'triple' or '*2' def triple(number): return number * 3 From 6742d7b9b685a730cb6a202e76770bf88fe9b33a Mon Sep 17 00:00:00 2001 From: pathywang Date: Mon, 17 Aug 2026 08:36:30 +0100 Subject: [PATCH 12/14] change string to list --- Spring 5 exercises/refactor.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Spring 5 exercises/refactor.py b/Spring 5 exercises/refactor.py index 90e734736..46e42531d 100644 --- a/Spring 5 exercises/refactor.py +++ b/Spring 5 exercises/refactor.py @@ -20,7 +20,7 @@ class Laptop: def find_possible_laptops(laptops: List[Laptop], person: Person) -> List[Laptop]: possible_laptops = [] for laptop in laptops: - if laptop.operating_system == person.preferred_operating_system: + if person.preferred_operating_system in laptop.operating_system: possible_laptops.append(laptop) return possible_laptops From 9688c02b70e2e061060412a294493c1e9bf039e4 Mon Sep 17 00:00:00 2001 From: pathywang Date: Mon, 17 Aug 2026 08:45:23 +0100 Subject: [PATCH 13/14] correction --- Spring 5 exercises/operation.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/Spring 5 exercises/operation.py b/Spring 5 exercises/operation.py index cbe61103a..0e67dd0d4 100644 --- a/Spring 5 exercises/operation.py +++ b/Spring 5 exercises/operation.py @@ -1,6 +1,5 @@ from dataclasses import dataclass from enum import Enum -from typing import List import sys @@ -50,13 +49,14 @@ class Laptop: print("- Arch Linux") print("- macOS") -os_input = input("Preferred operating system: ") +while True: + os_input = input("Preferred operating system: ") -try: - preferred_os = OperatingSystem(os_input) -except ValueError: - print("Error: invalid operating system.", file=sys.stderr) - sys.exit(1) + try: + preferred_os = OperatingSystem(os_input) + break + except ValueError: + print("Please choose Ubuntu, Arch Linux, or macOS.") # Create the person person = Person(name, age, preferred_os) From b1c18c34644ac6041d62cfefb5ad9a8ad30715da Mon Sep 17 00:00:00 2001 From: pathywang Date: Mon, 17 Aug 2026 09:07:03 +0100 Subject: [PATCH 14/14] add type --- Spring 5 exercises/checktype.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Spring 5 exercises/checktype.py b/Spring 5 exercises/checktype.py index de8a98795..55bdae46c 100644 --- a/Spring 5 exercises/checktype.py +++ b/Spring 5 exercises/checktype.py @@ -1,14 +1,14 @@ -def open_account(balances, name, amount): +def open_account (balances: dict[str, int], name: str, amount: int) -> None: balances[name] = amount -def sum_balances(accounts): +def sum_balances (accounts: dict[str, int]) -> 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): +def format_pence_as_string (total_pence: int) -> str: if total_pence < 100: return f"{total_pence}p" pounds = int(total_pence / 100)