Skip to content

Commit 1e06469

Browse files
Fix: improve user input handling and type hinting for operating system counts
1 parent 630ba6f commit 1e06469

1 file changed

Lines changed: 6 additions & 6 deletions

File tree

prep-exercises/h_enums.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import sys
22
from dataclasses import dataclass
33
from enum import Enum
4-
from typing import List
4+
from typing import List, Tuple
55
from collections import Counter
66

77
class OperatingSystem(Enum):
@@ -58,7 +58,7 @@ def find_possible_laptops(available_laptops: List[Laptop], current_person: Perso
5858

5959
def user_prompt() -> Person:
6060
try:
61-
name = str(input("Please enter your first name: "))
61+
name = (input("Please enter your first name: ")).strip()
6262
if not name.isalpha():
6363
raise ValueError("Name must contain only alphabetic characters.")
6464

@@ -97,7 +97,7 @@ def main() -> None:
9797
matching_laptops = find_possible_laptops(laptops, user_details)
9898

9999
# get the counts of the laptops for an OS e.g. os_counts = {OperatingSystem.MACOS: 4,OperatingSystem.ARCH: 2,OperatingSystem.UBUNTU: 3}
100-
os_counts = Counter(laptop.operating_system for laptop in laptops)
100+
os_counts: Counter[OperatingSystem] = Counter(laptop.operating_system for laptop in laptops)
101101

102102
print(f"\n{user_details.name}, there are {len(matching_laptops)} laptops available with your preferred operating system ({user_details.preferred_operating_systems.value}).\n")
103103

@@ -111,9 +111,9 @@ def main() -> None:
111111
# get the count of laptops for users preferred OS
112112
user_os_count = os_counts[user_details.preferred_operating_systems]
113113

114-
# find the OSes with more laptops available
115-
alternative_os = [(os, count) for os, count in os_counts.items() if count > user_os_count]
116-
114+
# sort alternative OS so most available OS is listed first
115+
alternative_os = sorted([(os, count) for os, count in os_counts.items() if count > user_os_count], key=lambda x: x[1], reverse=True)
116+
117117
if alternative_os:
118118
print("\nHowever, there are more laptops available with the following operating systems:")
119119
for os, count in alternative_os:

0 commit comments

Comments
 (0)