Untitled
unknown
plain_text
a month ago
2.1 kB
5
Indexable
def add_to_shopping_list(item, shopping_list, available_stock): if item in available_stock: shopping_list.append(item) return f"'{item}' added to your shopping list." else: return f"Error: '{item}' is not available in stock." def check_items_in_stock(shopping_list, available_stock): shopping_set = set(shopping_list) unavailable_items = shopping_set - available_stock if unavailable_items: return f"The following items are not available: {', '.join(unavailable_items)}" else: return "All items in your shopping list are available!" # Main program def main(): # Available stock (set) available_stock = {"apples", "bananas", "carrots", "milk", "bread", "eggs"} # Shopping list (list) shopping_list = [] print("Welcome to the Grocery Shopping System!") print(f"Available stock: {', '.join(available_stock)}\n") while True: print("\nOptions:") print("1. Add an item to the shopping list") print("2. Check if items in your shopping list are in stock") print("3. View your shopping list") print("4. Exit") try: choice = int(input("Enter your choice (1-4): ")) if choice == 1: item = input("Enter the item you want to add: ").lower() result = add_to_shopping_list(item, shopping_list, available_stock) print(result) elif choice == 2: result = check_items_in_stock(shopping_list, available_stock) print(result) elif choice == 3: print(f"Your shopping list: {', '.join(shopping_list) if shopping_list else 'No items yet.'}") elif choice == 4: print("Exiting the program. Happy shopping!") break else: print("Invalid choice. Please enter a number between 1 and 4.") except ValueError: print("Error: Please enter a valid number!") # Run the main program if __name__ == "__main__": main()
Editor is loading...
Leave a Comment