1+ from enum import Enum
2+ import sys
3+
4+ # Define the possible operating systems as an enum
5+ class OperatingSystem (Enum ):
6+ MACOS = "MacOS"
7+ ARCH = "Arch"
8+ UBUNTU = "Ubuntu"
9+
10+ # List of laptops available in the library
11+ laptops = [
12+ {"id" : 1 , "manufacturer" : "Dell" , "model" : "XPS" , "os" : OperatingSystem .ARCH },
13+ {"id" : 2 , "manufacturer" : "Dell" , "model" : "XPS" , "os" : OperatingSystem .UBUNTU },
14+ {"id" : 3 , "manufacturer" : "Dell" , "model" : "XPS" , "os" : OperatingSystem .UBUNTU },
15+ {"id" : 4 , "manufacturer" : "Apple" , "model" : "MacBook" , "os" : OperatingSystem .MACOS },
16+ {"id" : 5 , "manufacturer" : "Dell" , "model" : "Inspiron" , "os" : OperatingSystem .UBUNTU },
17+ ]
18+
19+ def get_user_preferences ():
20+ """Get the user's name and preferred operating system"""
21+ name = input ("Enter your name: " ).strip ()
22+ if not name :
23+ print ("Error: Name cannot be empty" , file = sys .stderr )
24+ sys .exit (1 )
25+
26+ print ("\n Available operating systems:" )
27+ print ("• MacOS" )
28+ print ("• Arch" )
29+ print ("• Ubuntu" )
30+
31+ os_choice = input ("Enter your preferred operating system: " ).strip ().lower ()
32+
33+ # Convert string to OperatingSystem enum
34+ if os_choice == "macos" :
35+ return name , OperatingSystem .MACOS
36+ elif os_choice == "arch" :
37+ return name , OperatingSystem .ARCH
38+ elif os_choice == "ubuntu" :
39+ return name , OperatingSystem .UBUNTU
40+ else :
41+ print (f"Error: '{ os_choice } ' is not a valid operating system" , file = sys .stderr )
42+ print ("Please choose from: macos, arch, ubuntu" , file = sys .stderr )
43+ sys .exit (1 )
44+
45+ def count_laptops_by_os ():
46+ """Count how many laptops we have for each operating system"""
47+ counts = {}
48+ for os in OperatingSystem :
49+ counts [os ] = 0
50+
51+ for laptop in laptops :
52+ counts [laptop ["os" ]] += 1
53+
54+ return counts
55+
56+ def main ():
57+ print ("=== Library Laptop Finder ===" )
58+
59+ # Get user input
60+ name , preferred_os = get_user_preferences ()
61+
62+ # Count laptops for each OS
63+ os_counts = count_laptops_by_os ()
64+
65+ # Show results to user
66+ user_laptop_count = os_counts [preferred_os ]
67+
68+ print (f"\n Hello { name } !" )
69+ print (f"We have { user_laptop_count } laptop(s) with { preferred_os .value } ." )
70+
71+ # Check if other OS have more laptops
72+ other_options = []
73+ for os , count in os_counts .items ():
74+ if os != preferred_os and count > user_laptop_count :
75+ other_options .append ((os , count ))
76+
77+ if other_options :
78+ print ("\n Tip: These operating systems have more laptops available:" )
79+ for os , count in other_options :
80+ print (f" • { os .value } : { count } laptops" )
81+
82+ # Show complete availability
83+ print (f"\n All available laptops:" )
84+ for os in OperatingSystem :
85+ count = os_counts [os ]
86+ print (f" • { os .value } : { count } laptop(s)" )
87+
88+ if __name__ == "__main__" :
89+ main ()
0 commit comments