-
-
Notifications
You must be signed in to change notification settings - Fork 92
Expand file tree
/
Copy pathenums.py
More file actions
126 lines (101 loc) · 3.2 KB
/
Copy pathenums.py
File metadata and controls
126 lines (101 loc) · 3.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
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
# Existing inventory list
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 get_user_input() -> Person:
name = input("Enter your name: ").strip()
if not name:
print("Error: Name cannot be empty.", file=sys.stderr)
sys.exit(1)
# 1. check the person's age is valid
try:
age = int(input("Enter your age: "))
if age < 0:
raise ValueError
except ValueError:
print("Error: Age must be a positive whole number.", file=sys.stderr)
sys.exit(1)
# 2. give the person a list of valid Operating Systems to choose from
print("\nAvailable Operating Systems:")
for os_option in OperatingSystem:
print(f"- {os_option.value}")
os_input = input("Enter your preferred operating system: ").strip()
# Try to match the user string to a valid Enum value
preferred_os = None
for os_option in OperatingSystem:
if os_option.value.lower() == os_input.lower():
preferred_os = os_option
break
if preferred_os is None:
print(f"Error: '{os_input}' is not a valid operating system.", file=sys.stderr)
sys.exit(1)
return Person(name=name, age=age, preferred_operating_system=preferred_os)
def main():
# Get valid user data
user = get_user_input()
# Count laptops for each operating system
os_counts = {os_type: 0 for os_type in OperatingSystem}
for laptop in laptops:
os_counts[laptop.operating_system] += 1
# Look up counts for user choice and find the maximum available
user_os = user.preferred_operating_system
user_os_count = os_counts[user_os]
most_available_os = max(os_counts, key=os_counts.get)
max_count = os_counts[most_available_os]
# Output results
print(f"\nHello {user.name}!")
print(f"The library has {user_os_count} laptop(s) running {user_os.value}.")
# Suggest alternative if another OS has more stock
if max_count > user_os_count:
print(
f"💡 Tip: If you choose {most_available_os.value}, we have {max_count} laptops available. You're more likely to get one quickly!"
)
if __name__ == "__main__":
main()