Skip to content

Commit 4ee4f65

Browse files
committed
Fix age input loop to re-prompt instead of exiting
1 parent 6d3a0c7 commit 4ee4f65

1 file changed

Lines changed: 67 additions & 16 deletions

File tree

sprint-5/enums_exercise.py

Lines changed: 67 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,13 @@
1313
from typing import List
1414
import sys
1515

16+
1617
class OperatingSystem(Enum):
1718
MACOS = "macOS"
1819
ARCH = "Arch Linux"
1920
UBUNTU = "Ubuntu"
2021

22+
2123
@dataclass(frozen=True)
2224
class Laptop:
2325
id: int
@@ -26,40 +28,89 @@ class Laptop:
2628
screen_size_in_inches: float
2729
operating_system: OperatingSystem
2830

31+
2932
laptops: List[Laptop] = [
30-
Laptop(id=1, manufacturer="Dell", model="XPS", screen_size_in_inches=13, operating_system=OperatingSystem.ARCH),
31-
Laptop(id=2, manufacturer="Dell", model="XPS", screen_size_in_inches=15, operating_system=OperatingSystem.UBUNTU),
32-
Laptop(id=3, manufacturer="Dell", model="XPS", screen_size_in_inches=15, operating_system=OperatingSystem.UBUNTU),
33-
Laptop(id=4, manufacturer="Apple", model="macBook", screen_size_in_inches=13, operating_system=OperatingSystem.MACOS),
33+
Laptop(
34+
id=1,
35+
manufacturer="Dell",
36+
model="XPS",
37+
screen_size_in_inches=13,
38+
operating_system=OperatingSystem.ARCH,
39+
),
40+
Laptop(
41+
id=2,
42+
manufacturer="Dell",
43+
model="XPS",
44+
screen_size_in_inches=15,
45+
operating_system=OperatingSystem.UBUNTU,
46+
),
47+
Laptop(
48+
id=3,
49+
manufacturer="Dell",
50+
model="XPS",
51+
screen_size_in_inches=15,
52+
operating_system=OperatingSystem.UBUNTU,
53+
),
54+
Laptop(
55+
id=4,
56+
manufacturer="Apple",
57+
model="macBook",
58+
screen_size_in_inches=13,
59+
operating_system=OperatingSystem.MACOS,
60+
),
3461
]
3562

63+
3664
def convert_os(user_input: str) -> OperatingSystem:
3765
for os_value in OperatingSystem:
3866
if user_input.lower() == os_value.value.lower():
3967
return os_value
40-
print(f"Error: '{user_input}' is not a valid operating system.", file=sys.stderr)
41-
sys.exit(1)
68+
69+
print(f"Error: '{user_input}' is not a valid operating system.", file=sys.stderr)
70+
print("Please enter one of the following: macOS, Arch Linux, Ubuntu.")
71+
sys.exit(1)
72+
4273

4374
# Get user input
4475
name = input("Enter your name: ").strip()
45-
age_input = input("Enter your age: ").strip()
46-
if not age_input.isdigit():
47-
print(f"Error: '{age_input}' age must be a number.", file=sys.stderr)
48-
sys.exit(1)
49-
age = int(age_input)
5076

51-
os_input = input("Enter your preferred operating system (macOS, Arch Linux, Ubuntu): ").strip()
77+
while True:
78+
age_input = input("Enter your age: ").strip()
79+
80+
try:
81+
age = int(age_input)
82+
if age < 0:
83+
print("Age cannot be negative. Please enter a valid age.", file=sys.stderr)
84+
continue
85+
break
86+
except ValueError:
87+
print(
88+
f"Error: '{age_input}' is not a valid number. Please enter a valid number.",
89+
file=sys.stderr,
90+
)
91+
92+
print("Your age is:", age)
93+
94+
os_input = input(
95+
"Enter your preferred operating system (macOS, Arch Linux, Ubuntu): "
96+
).strip()
5297
preferred_os = convert_os(os_input)
5398

5499
# Count laptops with the preferred operating system
55-
matching_laptops = [laptop for laptop in laptops if laptop.operating_system == preferred_os]
56-
print(f"\nHi {name}, there are {len(matching_laptops)} laptops available with {preferred_os.value}.")
100+
matching_laptops = [
101+
laptop for laptop in laptops if laptop.operating_system == preferred_os
102+
]
103+
print(
104+
f"\nHi {name}, there are {len(matching_laptops)} laptops available with {preferred_os.value}."
105+
)
57106

58107
# Check if there are better laptops with a different operating system
59-
counts ={os: 0 for os in OperatingSystem}
108+
counts = {os: 0 for os in OperatingSystem}
60109
for laptop in laptops:
61110
counts[laptop.operating_system] += 1
62111

63112
best_os = max(counts, key=counts.get)
64113
if best_os != preferred_os:
65-
print(f"if you’re willing to accept {best_os.value} you’re more likely to get a laptop, as there are {counts[best_os]} available.")
114+
print(
115+
f"if you’re willing to accept {best_os.value} you’re more likely to get a laptop, as there are {counts[best_os]} available."
116+
)

0 commit comments

Comments
 (0)