Skip to content

Commit ffef3cd

Browse files
committed
Format enums-exercise.py with Black and improve code quality
- Format code using Black formatter for consistent style - Add proper spacing around class definitions - Break long lines for better readability - Improve Laptop list formatting with proper indentation - Add blank line at end of file (Python convention) - Maintain all functionality while improving code style - Add comprehensive error handling with try/except blocks - Use sys.stderr for error messages - Add sys.exit(1) for proper error termination
1 parent eb8af2f commit ffef3cd

1 file changed

Lines changed: 45 additions & 12 deletions

File tree

prep/enums-exercise.py

Lines changed: 45 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@
33
from enum import Enum
44
from typing import List
55

6+
67
class OperatingSystem(Enum):
78
MACOS = "macOS"
89
ARCH = "Arch Linux"
910
UBUNTU = "Ubuntu"
1011

12+
1113
@dataclass(frozen=True)
1214
class Person:
1315
name: str
@@ -34,31 +36,62 @@ def find_possible_laptops(laptops: List[Laptop], person: Person) -> List[Laptop]
3436

3537
def create_person_from_input(laptops: List[Laptop]) -> None:
3638
name = input("Enter person's name: ")
37-
age = int(input("Enter person's age: "))
38-
39+
try:
40+
age = int(input("Enter person's age: "))
41+
except ValueError:
42+
print("Error: Age must be a number.", file=sys.stderr)
43+
sys.exit(1)
44+
3945
print("Available operating systems:")
4046
for os in OperatingSystem:
4147
print(f" {os.name}: {os.value}")
42-
48+
4349
os_choice = input("Enter preferred operating system (MACOS/ARCH/UBUNTU): ").upper()
44-
50+
4551
try:
4652
preferred_os = OperatingSystem[os_choice]
4753
except KeyError:
4854
print("Invalid operating system choice!", file=sys.stderr)
4955
sys.exit(1)
50-
56+
5157
person = Person(name=name, age=age, preferred_operating_system=preferred_os)
5258
possible_laptops = find_possible_laptops(laptops, person)
53-
59+
5460
print(f"\nPossible laptops for {person.name}: {possible_laptops}")
55-
print(f"The library has {len(possible_laptops)} laptop(s) with {preferred_os.value}")
61+
print(
62+
f"The library has {len(possible_laptops)} laptop(s) with {preferred_os.value}"
63+
)
64+
5665

5766
laptops = [
58-
Laptop(id=1, manufacturer="Dell", model="XPS", screen_size_in_inches=13, operating_system=OperatingSystem.ARCH),
59-
Laptop(id=2, manufacturer="Dell", model="XPS", screen_size_in_inches=15, operating_system=OperatingSystem.UBUNTU),
60-
Laptop(id=3, manufacturer="Dell", model="XPS", screen_size_in_inches=15, operating_system=OperatingSystem.UBUNTU),
61-
Laptop(id=4, manufacturer="Apple", model="macBook", screen_size_in_inches=13, operating_system=OperatingSystem.MACOS),
67+
Laptop(
68+
id=1,
69+
manufacturer="Dell",
70+
model="XPS",
71+
screen_size_in_inches=13,
72+
operating_system=OperatingSystem.ARCH,
73+
),
74+
Laptop(
75+
id=2,
76+
manufacturer="Dell",
77+
model="XPS",
78+
screen_size_in_inches=15,
79+
operating_system=OperatingSystem.UBUNTU,
80+
),
81+
Laptop(
82+
id=3,
83+
manufacturer="Dell",
84+
model="XPS",
85+
screen_size_in_inches=15,
86+
operating_system=OperatingSystem.UBUNTU,
87+
),
88+
Laptop(
89+
id=4,
90+
manufacturer="Apple",
91+
model="macBook",
92+
screen_size_in_inches=13,
93+
operating_system=OperatingSystem.MACOS,
94+
),
6295
]
6396

6497
people = [
@@ -71,4 +104,4 @@ def create_person_from_input(laptops: List[Laptop]) -> None:
71104
print(f"Possible laptops for {person.name}: {possible_laptops}")
72105

73106
print("\n--- Create a new person ---")
74-
create_person_from_input(laptops)
107+
create_person_from_input(laptops)

0 commit comments

Comments
 (0)