Skip to content

Commit 74fada8

Browse files
committed
exercise 11 completed
1 parent 978812d commit 74fada8

1 file changed

Lines changed: 82 additions & 1 deletion

File tree

‎Sprint 5 Prep Exercises/exercise_11.py‎

Lines changed: 82 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,85 @@
55
# Tells the user how many laptops the library has that have that operating system.
66
# If there is an operating system that has more laptops available, tells the user that if they’re willing to accept that operating system they’re more likely to get a laptop.
77

8-
# You should convert the age and preferred operating system input from the user into more constrained types as quickly as possible, and should output errors to stderr and terminate the program with a non-zero exit code if the user input bad values.
8+
# You should convert the age and preferred operating system input from the user into more constrained types as quickly as possible,
9+
# and should output errors to stderr and terminate the program with a non-zero exit code if the user input bad values.
10+
11+
# ----- Imports
12+
from dataclasses import dataclass
13+
from enum import Enum
14+
from typing import List
15+
import sys
16+
17+
# ----- Classes and Enums
18+
class OperatingSystem(Enum):
19+
MACOS = "macOS"
20+
ARCH = "Arch Linux"
21+
UBUNTU = "Ubuntu"
22+
WINDOWS = "Windows"
23+
24+
@dataclass(frozen=True)
25+
class Person:
26+
name: str
27+
age: int
28+
preferred_operating_system: OperatingSystem
29+
30+
@dataclass(frozen=True)
31+
class Laptop:
32+
id: int
33+
operating_system: OperatingSystem
34+
35+
# ----- Functions
36+
def count_laptops(laptops: list[Laptop], laptop_counts: dict[str, int]):
37+
for laptop in laptops:
38+
laptop_counts[laptop.operating_system] += 1
39+
40+
def check_laptop_abundance(operating_system: OperatingSystem, laptop_counts: dict[OperatingSystem, int]):
41+
abundant_laptops = []
42+
for laptop in laptop_counts:
43+
if laptop_counts[laptop] > laptop_counts[operating_system]:
44+
abundant_laptops.append(laptop.value)
45+
if len(abundant_laptops) > 0:
46+
print("\nIf you are willing to accept another operating system you may get a laptop sooner.")
47+
print("We have more laptops available with the following OS:")
48+
for laptop in abundant_laptops:
49+
print(laptop)
50+
51+
# ----- Data and Constants
52+
laptops = [
53+
Laptop(id=1, operating_system=OperatingSystem.ARCH),
54+
Laptop(id=2, operating_system=OperatingSystem.ARCH),
55+
Laptop(id=3, operating_system=OperatingSystem.UBUNTU),
56+
Laptop(id=4, operating_system=OperatingSystem.UBUNTU),
57+
Laptop(id=5, operating_system=OperatingSystem.UBUNTU),
58+
Laptop(id=6, operating_system=OperatingSystem.UBUNTU),
59+
Laptop(id=7, operating_system=OperatingSystem.MACOS),
60+
Laptop(id=8, operating_system=OperatingSystem.MACOS),
61+
]
62+
laptop_counts = {
63+
OperatingSystem.MACOS: 0,
64+
OperatingSystem.ARCH: 0,
65+
OperatingSystem.UBUNTU: 0,
66+
OperatingSystem.WINDOWS: 0
67+
}
68+
69+
# ----- Script
70+
user_name = input("Please enter your full name:\n")
71+
user_age_str = input("Please enter your age:\n")
72+
73+
try:
74+
user_age = int(user_age_str)
75+
except ValueError:
76+
sys.exit("Error: Age should be a number.")
77+
78+
user_operating_system_str = input("Please enter your preferred operating system (options: ARCH, UBUNTU, MACOS, WINDOWS):\n")
79+
80+
if user_operating_system_str not in OperatingSystem.__members__:
81+
sys.exit("Error: Operating system should be written in all caps from given options.")
82+
else: user_operating_system = OperatingSystem[user_operating_system_str]
83+
84+
user = Person(user_name, user_age, user_operating_system)
85+
86+
count_laptops(laptops, laptop_counts)
87+
print(f"\nThe number of available laptops with {user_operating_system.value} is: {laptop_counts[user_operating_system]}")
88+
89+
check_laptop_abundance(user_operating_system, laptop_counts)

0 commit comments

Comments
 (0)