Problem Solving:
Direction: Write a program in Python that imitates a vending machine. The program should ask
the user which drink he/she wants to buy: Soda, Juice or Sports drink
def vending_machine():
menu = {
"soda": {"small": 7, "medium": 12, "large": 18},
"juice": {"small": 5, "medium": 9, "large": 13},
"sports drink": {"small": 9, "medium": 18, "large": 25}
}
print("Welcome to the Vending Machine!")
print("Available drinks: Soda, Juice, Sports drink")
drink = input("Please select a drink: ").strip().lower()
if drink not in menu:
print("Invalid drink selected.")
return
size = input("Choose a size (small, medium, large): ").strip().lower()
if size not in menu[drink]:
print("Invalid size selected.")
return
price = menu[drink][size]
print(f"The price for {size} {[Link]()} is {price} php.")
total_inserted = 0
while total_inserted < price:
try:
coin = int(input("Insert coin (1, 5, or 10 php): "))
if coin not in [1, 5, 10]:
print("Invalid coin. Try again.")
continue
total_inserted += coin
print(f"Total inserted: {total_inserted} php")
except ValueError:
print("Please enter a valid number.")
change = total_inserted - price
print(f"Dispensing your {size} {[Link]()}... Enjoy!")
if change > 0:
print(f"Don't forget your change: {change} php")
# Run the vending machine
vending_machine()