-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshopping_list.py
More file actions
131 lines (107 loc) · 3.91 KB
/
shopping_list.py
File metadata and controls
131 lines (107 loc) · 3.91 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
127
128
129
130
def display_menu():
print("\nGrocery Shopping List Manager")
print("1. Add an Item")
print("2. Remove an Item")
print("3. View the List")
print("4. Calculate Item Statistics")
print("5. Sort the List")
print("6. Search for an Item")
print("7. Exit the Program")
def add_item(grocery_list):
item = input("Enter the item name: ").strip()
if not item:
print("Invalid input. Item name cannot be empty.")
return
try:
quantity = int(input("Enter the quantity: "))
if quantity <= 0:
print("Quantity must be greater than zero.")
return
grocery_list[item] = grocery_list.get(item, 0) + quantity
print(f"{quantity} {item}(s) added to the list.")
except ValueError:
print("Invalid input. Please enter a valid number for quantity.")
def remove_item(grocery_list):
item = input("Enter the item name to remove: ").strip()
if item not in grocery_list:
print("Item not found in the list.")
return
try:
quantity = int(input("Enter the quantity to remove: "))
if quantity <= 0:
print("Quantity must be greater than zero.")
return
if quantity >= grocery_list[item]:
del grocery_list[item]
print(f"{item} removed from the list.")
else:
grocery_list[item] -= quantity
print(f"{quantity} {item}(s) removed from the list.")
except ValueError:
print("Invalid input. Please enter a valid number for quantity.")
def view_list(grocery_list):
if not grocery_list:
print("Your grocery list is empty.")
else:
print("\nGrocery List:")
for item, quantity in grocery_list.items():
print(f"{item}: {quantity}")
def calculate_statistics(grocery_list):
total_quantity = sum(grocery_list.values())
total_unique_items = len(grocery_list)
print(f"Total quantity of all items: {total_quantity}")
print(f"Total number of unique items: {total_unique_items}")
def sort_list(grocery_list):
if not grocery_list:
print("The list is empty, nothing to sort.")
return
print("Choose sorting option:")
print("1. Alphabetically (A-Z)")
print("2. Alphabetically (Z-A)")
print("3. By Quantity (Low to High)")
print("4. By Quantity (High to Low)")
choice = input("Enter your choice: ")
if choice == "1":
sorted_items = sorted(grocery_list.items())
elif choice == "2":
sorted_items = sorted(grocery_list.items(), reverse=True)
elif choice == "3":
sorted_items = sorted(grocery_list.items(), key=lambda x: x[1])
elif choice == "4":
sorted_items = sorted(grocery_list.items(), key=lambda x: x[1], reverse=True)
else:
print("Invalid choice.")
return
print("\nSorted Grocery List:")
for item, quantity in sorted_items:
print(f"{item}: {quantity}")
def search_item(grocery_list):
item = input("Enter the item name to search: ").strip()
if item in grocery_list:
print(f"{item} is in the list with quantity: {grocery_list[item]}")
else:
print("Item not found in the list.")
def main():
grocery_list = {}
while True:
display_menu()
choice = input("Enter your choice: ")
if choice == "1":
add_item(grocery_list)
elif choice == "2":
remove_item(grocery_list)
elif choice == "3":
view_list(grocery_list)
elif choice == "4":
calculate_statistics(grocery_list)
elif choice == "5":
sort_list(grocery_list)
elif choice == "6":
search_item(grocery_list)
elif choice == "7":
print("Exiting the program. Goodbye!")
break
else:
print("Invalid choice. Please enter a number between 1 and 7.")
if __name__ == "__main__":
main()