Question
object oriented programming in python . i want to create project of vending machine with 3 classes and asking the user at the end if
object oriented programming in python . i want to create project of vending machine with 3 classes and asking the user at the end if he wants anything else .if yes start from the begining if no say bye . you can use this code or new one what ever easy for you and thank you.
import sys
class Item:
def __init__(self, name, price):
self.name = name
self.price = price
class VendingMachine:
def __init__(self):
self.items = [
Item("Tea", 0.50),
Item("Coffee", 1.00),
Item("Coke", 1.50),
Item("Orange Juice", 1.00)
]
self.money_inserted = 0.00
def display_items(self):
for code, item in enumerate(self.items, start=1):
print(f"[{code}] - {item.name} (${item.price:.2f})")
def insert_money(self, money):
if money <= 0.00:
raise ValueError
self.money_inserted += money
def main():
vending_machine = VendingMachine()
vending_machine.display_items()
while True:
try:
user_selection = int(input("Please enter the desired item code: "))
except ValueError:
continue
if user_selection in range(1, len(vending_machine.items)+1):
break
item = vending_machine.items[user_selection-1]
print(f"You've selected \"{item.name}\" - the price is ${item.price:.2f}")
while vending_machine.money_inserted < item.price:
print(f"You've inserted ${vending_machine.money_inserted:.2f} into the machine so far.")
while True:
try:
money_to_insert = float(input("Please enter the amount of money you'd like to insert: "))
vending_machine.insert_money(money_to_insert)
except ValueError:
continue
else:
break
print(f"Thank you! Please take your \"{item.name}\".")
print(f"The remaining change in the machine is ${vending_machine.money_inserted - item.price:.2f}.")
return 0
if __name__ == "__main__":
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started