Skip to content

Commit 67c5dff

Browse files
committed
Add interactive user input to enums-exercise.py
- Add create_person_from_input() function for interactive person creation - Prompt user for name, age, and preferred operating system - Display available OS options from OperatingSystem Enum - Validate user input with try/except for invalid OS choices - Create Person instance and find matching laptops - Display matching laptops for newly created person - Maintain existing hardcoded people and laptop matching - Add interactive section at end to test user input functionality
1 parent 535683f commit 67c5dff

1 file changed

Lines changed: 29 additions & 5 deletions

File tree

prep/enums-exercise.py

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,26 @@ def find_possible_laptops(laptops: List[Laptop], person: Person) -> List[Laptop]
3131
return possible_laptops
3232

3333

34-
people = [
35-
Person(name="Imran", age=22, preferred_operating_system=OperatingSystem.UBUNTU),
36-
Person(name="Eliza", age=34, preferred_operating_system=OperatingSystem.ARCH),
37-
]
34+
def create_person_from_input(laptops: List[Laptop]) -> None:
35+
name = input("Enter person's name: ")
36+
age = int(input("Enter person's age: "))
37+
38+
print("Available operating systems:")
39+
for os in OperatingSystem:
40+
print(f" {os.name}: {os.value}")
41+
42+
os_choice = input("Enter preferred operating system (MACOS/ARCH/UBUNTU): ").upper()
43+
44+
try:
45+
preferred_os = OperatingSystem[os_choice]
46+
except KeyError:
47+
print("Invalid operating system choice!")
48+
return
49+
50+
person = Person(name=name, age=age, preferred_operating_system=preferred_os)
51+
possible_laptops = find_possible_laptops(laptops, person)
52+
print(f"Possible laptops for {person.name}: {possible_laptops}")
53+
3854

3955
laptops = [
4056
Laptop(id=1, manufacturer="Dell", model="XPS", screen_size_in_inches=13, operating_system=OperatingSystem.ARCH),
@@ -43,6 +59,14 @@ def find_possible_laptops(laptops: List[Laptop], person: Person) -> List[Laptop]
4359
Laptop(id=4, manufacturer="Apple", model="macBook", screen_size_in_inches=13, operating_system=OperatingSystem.MACOS),
4460
]
4561

62+
people = [
63+
Person(name="Imran", age=22, preferred_operating_system=OperatingSystem.UBUNTU),
64+
Person(name="Eliza", age=34, preferred_operating_system=OperatingSystem.ARCH),
65+
]
66+
4667
for person in people:
4768
possible_laptops = find_possible_laptops(laptops, person)
48-
print(f"Possible laptops for {person.name}: {possible_laptops}")
69+
print(f"Possible laptops for {person.name}: {possible_laptops}")
70+
71+
print("\n--- Create a new person ---")
72+
create_person_from_input(laptops)

0 commit comments

Comments
 (0)