use this as a startshoppinglist = [] print("Welcome to the Shopping List !") while True:…
- User
use this as a startshopping_list = []
print("Welcome to the Shopping List !")
while True: print("\nOptions:") print("1. View shopping list") print("2. Add item to shopping list") print("3. Remove specific item from shopping list") print("4. Remove the last item from shopping list") print("5. Exit")
choice = input("Please enter your choice (1-5): ") if choice == "1": if shopping_list: print("\nYour Shopping List:") for item in shopping_list: print(f"- {item}") to do this without using # Task:Create a python program that behaves like a shopping list. It should include the following functionality.
Reference code from the previous lesson for help.
Greet the user Give the user the option to view the list Give the user the option to add an item to the list Give the user the option to remove a specific item from the list Give the user the option to remove the last item from the list
- Assistant
Certainly! Below is a complete Python program that implements a shopping list with the functionality you specified. It greets the user and provides options to view, add, and remove items from the shopping list.
shopping_list = [] print("Welcome to the Shopping List!") while True: print("\nOptions:") print("1. View shopping list") print("2. Add item to shopping list") print("3. Remove specific item from shopping list") print("4. Remove the last item from shopping list") print("5. Exit") choice = input("Please enter your choice (1-5): ") if choice == "1": if shopping_list: print("\nYour Shopping List:") for item in shopping_list: print(f"- {item}") else: print("\nYour shopping list is empty.") elif choice == "2": item = input("Enter the item you want to add: ") shopping_list.append(item) print(f"{item} has been added to your shopping list.") elif choice == "3": item = input("Enter the item you want to remove: ") if item in shopping_list: shopping_list.remove(item) print(f"{item} has been removed from your shopping list.") else: print(f"{item} is not in your shopping list.") elif choice == "4": if shopping_list: removed_item = shopping_list.pop() # Removes the last item print(f"{removed_item} has been removed from your shopping list.") else: print("Your shopping list is empty, there's nothing to remove.") elif choice == "5": print("Exiting the Shopping List. Goodbye!") break else: print("Invalid choice. Please enter a number between 1 and 5.")Explanation of Features:
- Viewing the Shopping List: The program checks if the list is empty or not before displaying.
- Adding Items: Users can enter items to be added to the list.
- Removing Specific Items: Users can specify which item they wish to remove from the list.
- Removing the Last Item: Users can remove the most recently added item with a simple removal command.
- Exit Option: Users can exit the program gracefully.
You can run this program in any Python environment. Enjoy managing your shopping list!