Level 1
Level 1
def __init__(self):
self.menu = {
'Pizza': 10.99,
'Burger': 5.99,
'Pasta': 7.99,
'Salad': 4.99,
'Soda': 1.99
}
self.order = []
self.total_cost = 0
def display_menu(self):
print("\n--- Menu ---")
for item, price in self.menu.items():
print(f"{item}: ${price:.2f}")
def take_order(self):
while True:
self.display_menu()
choice = input("\nEnter the name of the food to order (or 'done' to
finish): ").title()
if choice == 'Done':
break
elif choice in self.menu:
self.order.append(choice)
self.total_cost += self.menu[choice]
print(f"{choice} added to your order.")
else:
print("Invalid choice. Please try again.")
def show_order(self):
if not self.order:
print("\nYour order is empty!")
else:
print("\n--- Your Order ---")
for item in self.order:
print(item)
print(f"Total cost: ${self.total_cost:.2f}")
def checkout(self):
self.show_order()
if self.order:
payment = float(input("\nEnter payment amount: $"))
if payment >= self.total_cost:
change = payment - self.total_cost
print(f"Thank you for your order! Your change is ${change:.2f}")
else:
print("Insufficient funds! Please enter a valid payment amount.")
else:
print("No items ordered. Please order something first.")
if __name__ == "__main__":
system = FoodOrderingSystem()
system.take_order()
system.checkout()
How the Code Works:
Menu Initialization: The FoodOrderingSystem class is initialized with a dictionary
of food items and their prices.
Display Menu: The display_menu() method prints the available items and their
prices.
Taking Orders: The take_order() method allows the user to input the food items they
wish to order. The loop continues until the user types 'done'.
Show Order: The show_order() method displays all the items in the order and the
total cost.
Checkout: The checkout() method shows the final order, asks for payment, and checks
if the payment is sufficient. It also calculates the change if the payment is
higher than the total cost.
Example Output:
vbnet
Copy code
Welcome to the Food Ordering System!
Enter the name of the food to order (or 'done' to finish): Pizza
Pizza added to your order.
Enter the name of the food to order (or 'done' to finish): Burger
Burger added to your order.
Enter the name of the food to order (or 'done' to finish): done
By messaging ChatGPT, you agree to our Terms and have read our Privacy Policy.
Don't share sensitive info. Chats may be reviewed and used to train our models.
Learn more
ChatGPT can make