0% found this document useful (0 votes)
14 views2 pages

Python Vending Machine Program

The document provides a Python program that simulates a vending machine allowing users to select a drink (Soda, Juice, or Sports drink) and size (small, medium, large). It calculates the price based on the selection and prompts the user to insert coins until the total meets or exceeds the price. The program also handles invalid selections and provides change if applicable.

Uploaded by

adaliacosejo02
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views2 pages

Python Vending Machine Program

The document provides a Python program that simulates a vending machine allowing users to select a drink (Soda, Juice, or Sports drink) and size (small, medium, large). It calculates the price based on the selection and prompts the user to insert coins until the total meets or exceeds the price. The program also handles invalid selections and provides change if applicable.

Uploaded by

adaliacosejo02
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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()

You might also like