Skip to content

Commit 1b2d91c

Browse files
committed
inhancing the UI and validation in enums.py file
1 parent e9ba778 commit 1b2d91c

1 file changed

Lines changed: 13 additions & 12 deletions

File tree

‎sprint5-prep-exercises/enums.py‎

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,23 +33,28 @@ class Laptop:
3333
]
3434

3535

36-
# Get the person's name
3736
name = input("What is your name? ")
3837

39-
40-
# Get and convert age
4138
try:
4239
age = int(input("What is your age? "))
4340
except ValueError:
4441
print("Age must be a number.", file=sys.stderr)
4542
sys.exit(1)
4643

4744

48-
# Get and convert operating system
45+
print("Available operating systems:")
46+
for os in OperatingSystem:
47+
print(os.value)
48+
4949
try:
50-
os_input = input("What is your preferred operating system? ")
51-
operating_system = OperatingSystem(os_input)
52-
except ValueError:
50+
os_input = input("What is your preferred operating system? ").strip().lower()
51+
52+
operating_system = next(
53+
os for os in OperatingSystem
54+
if os.value.lower() == os_input
55+
)
56+
57+
except StopIteration:
5358
print("Invalid operating system.", file=sys.stderr)
5459
sys.exit(1)
5560

@@ -61,21 +66,18 @@ class Laptop:
6166
)
6267

6368

64-
# Count laptops with the person's preferred OS
6569
number_available = sum(
6670
laptop.operating_system == person.preferred_operating_system
6771
for laptop in laptops
6872
)
6973

70-
7174
print(
7275
f"We have {number_available} laptop(s) with "
7376
f"{person.preferred_operating_system.value}."
7477
)
7578

7679

77-
# Find the operating system with the most laptops
78-
counts = {}
80+
counts: dict[OperatingSystem, int] = {}
7981

8082
for os in OperatingSystem:
8183
counts[os] = sum(
@@ -86,7 +88,6 @@ class Laptop:
8688

8789
most_available_os = max(counts, key=counts.get)
8890

89-
9091
if most_available_os != person.preferred_operating_system:
9192
if counts[most_available_os] > number_available:
9293
print(

0 commit comments

Comments
 (0)