import menu
cart = [] #List to store items in the cart
def print_menu(menu):
print("\n=== MENU ===")
for num, drink in menu.items():
print(f"{num}. {drink['name']} - {drink['price']}")
def menu_inquiry(full_menu):
"""Lets user choose options; matcha, hojicha, view cart, or exit"""
while True:
print("What would you like to order?")
print("1. Matcha")
print("2. Hojicha")
print("3. View Cart/Checkout")
print("4. Exit")
choice = input("Enter your choice: ")
if choice == "1":
selected_menu = full_menu["matcha"]
print("\n=== MATCHA MENU ===")
print_menu(selected_menu)
handle_order(selected_menu)
elif choice == "2":
selected_menu = full_menu["hojicha"]
print("\n=== HOJICHA MENU ===")
print_menu(selected_menu)
handle_order(selected_menu)
elif choice == "3":
checkout(full_menu)
elif choice == "4":
print("Thank you for your visit!")
exit()
else:
print("Invalid choice. Please try again.")
def handle_order(selected_menu):
"""Prompts user to enter a drink they'd like to order.
Provides them with the description of the drink they'd like to order
Adds the selected item to the cart list"""
drink_choice = input("\n Enter the drink you would like to order: ")
if drink_choice.isdigit():
drink_choice = int(drink_choice)
if drink_choice in selected_menu:
drink = selected_menu[drink_choice]
print("\n Drink Details:")
print(f"Name: {drink['name']}")
print(f"Price: {drink['price']}")
print(f"Description: {drink['description']}")
add = input("\n Add to cart? (y/n): ").lower()
if add == "y":
cart.append(drink)
print(f"\n {drink['name']} added to cart!")
elif add == "n":
return
else:
print("Invalid choice. Please try again.")
handle_order(selected_menu)
else:
print("Invalid drink number. Please try again.")
handle_order(selected_menu)
else:
print("Invalid input. Please enter a valid drink number.")
handle_order(selected_menu)
def checkout(full_menu):
"""Checkout process: Program notifies if the cart is empty.
1. Lists out the items in the cart and provides the total price
2. User is able to exit the cart and return to the menu or proceed to checkout"""
total = 0
if not cart:
print("\n Your cart is empty.")
return
print("\n=== YOUR CART ===")
for i, item in enumerate(cart, 1):
price = int(item['price'].replace('RM', ''))
total += price
print(f"{i}. {item['name']} - {item['price']}")
print(f"\n Total: RM{total}")
confirm = input("\n Proceed to checkout? (y/n) | (r) to remove an item: ").lower()
if confirm == "y":
print("Order confirmed! Thank you for your purchase!")
cart.clear()
elif confirm == "n":
print("Order cancelled. Returning to menu...")
menu_inquiry(full_menu)
elif confirm == "r":
input("Select item to be removed: ")
else:
print("Invalid choice. Please try again.")
if __name__ == "__main__":
print("Welcome to Momocha!")
menu_inquiry(menu.full_menu)
Любые предложения по изменениям или реализации приветствуются.
Я делаю «торговую палатку» для магазина и пытаюсь реализовать функцию «удаления», чтобы удалять товары из корзины, но понятия не имею, с чего начать. [code]import menu cart = [] #List to store items in the cart
def print_menu(menu): print("\n=== MENU ===") for num, drink in menu.items(): print(f"{num}. {drink['name']} - {drink['price']}")
def menu_inquiry(full_menu): """Lets user choose options; matcha, hojicha, view cart, or exit"""
while True:
print("What would you like to order?") print("1. Matcha") print("2. Hojicha") print("3. View Cart/Checkout") print("4. Exit")
choice = input("Enter your choice: ")
if choice == "1": selected_menu = full_menu["matcha"] print("\n=== MATCHA MENU ===") print_menu(selected_menu) handle_order(selected_menu)
elif choice == "4": print("Thank you for your visit!") exit()
else: print("Invalid choice. Please try again.")
def handle_order(selected_menu): """Prompts user to enter a drink they'd like to order. Provides them with the description of the drink they'd like to order Adds the selected item to the cart list""" drink_choice = input("\n Enter the drink you would like to order: ")
if drink_choice.isdigit(): drink_choice = int(drink_choice) if drink_choice in selected_menu: drink = selected_menu[drink_choice] print("\n Drink Details:") print(f"Name: {drink['name']}") print(f"Price: {drink['price']}") print(f"Description: {drink['description']}")
add = input("\n Add to cart? (y/n): ").lower() if add == "y": cart.append(drink) print(f"\n {drink['name']} added to cart!")
def checkout(full_menu): """Checkout process: Program notifies if the cart is empty. 1. Lists out the items in the cart and provides the total price 2. User is able to exit the cart and return to the menu or proceed to checkout"""
total = 0
if not cart: print("\n Your cart is empty.") return
print("\n=== YOUR CART ===") for i, item in enumerate(cart, 1): price = int(item['price'].replace('RM', '')) total += price print(f"{i}. {item['name']} - {item['price']}")
print(f"\n Total: RM{total}")
confirm = input("\n Proceed to checkout? (y/n) | (r) to remove an item: ").lower() if confirm == "y": print("Order confirmed! Thank you for your purchase!") cart.clear() elif confirm == "n": print("Order cancelled. Returning to menu...") menu_inquiry(full_menu) elif confirm == "r": input("Select item to be removed: ")
else: print("Invalid choice. Please try again.")
if __name__ == "__main__": print("Welcome to Momocha!") menu_inquiry(menu.full_menu) [/code] Любые предложения по изменениям или реализации приветствуются.