0
Willing to receive any thoughts or comments on this Python Vending Machine.
So I just started the python intermediate course and decided to create a vending machine. If anyone has any advice or criticism, I am willing to hear it. Here's the code. https://www.sololearn.com/en/compiler-playground/c9nYk7OJaO3L
1 Réponse
0
class VendingMachine:
"""
A simple vending machine that dispenses items based on user input.
"""
def __init__(self, inventory):
"""
Initializes the vending machine with an inventory of items.
Args:
inventory (dict): A dictionary where keys are item names and values are tuples
containing (price, quantity).
"""
self.inventory = inventory
def display_items(self):
"""
Displays the available items and their prices.
"""
print("Available Items:")
for item, (price, quantity) in self.inventory.items():
print(f"- {item}: ${price:.2f} (Quantity: {quantity})")
def select_item(self):
"""
Prompts the user to select an item and validates the input.
"""
while True:
item = input("Enter the name of the item you want: ")
if item in self.inventory:
return item
else:
print("Invalid item. Please try again.")
def insert_money(self):
"""
Prompts the user to insert money and validates the input.
"""
https://www.virginiavadmv.com