Hi,
I have been working on a vending machine project which is outlined below.
After selecting 'b' in the first menu, I am having trouble with comparing the selected item cost with the total amount within the vending machine (self.total). I have worked out how to reference the product selected via a dictionary (VendingMachine.order_product), however this only works as a string and I cannot subtract the the value of the product (product.price), from the self.total - This can be seen at self.total -= {product.price} in VendingMachine.order_product
Once the order has been completed, and the product received, I want to subtract 1 product from the item.stock which can be selected in the first menu under option c - 'Transactions'. If there are none left in stock, I want to pull the item out of the dict via the .remove method I am assuming.
I don't have any friends who code, so its been tough getting this far! Any feedback would be appreciated!
Apologies if I did not explain it very well.
I have been working on a vending machine project which is outlined below.
After selecting 'b' in the first menu, I am having trouble with comparing the selected item cost with the total amount within the vending machine (self.total). I have worked out how to reference the product selected via a dictionary (VendingMachine.order_product), however this only works as a string and I cannot subtract the the value of the product (product.price), from the self.total - This can be seen at self.total -= {product.price} in VendingMachine.order_product
Once the order has been completed, and the product received, I want to subtract 1 product from the item.stock which can be selected in the first menu under option c - 'Transactions'. If there are none left in stock, I want to pull the item out of the dict via the .remove method I am assuming.
I don't have any friends who code, so its been tough getting this far! Any feedback would be appreciated!
Apologies if I did not explain it very well.
accepted_coins= [0.05, 0.10, 0.20, 0.50, 1.00, 2.00] options = ["a", "b", "c"] class Item: def __init__(self, code, name, price, amount): self.code = code self.name = name self.price = price self.amount = amount class VendingMachine: print("Machine booting up") def __init__(self): self.total = 0.00 self.items = { '1' : Item(1, "Tea", 0.50, 10), '2' : Item(2, "Coffee", 1.00, 10), "3" : Item(3, "Coke", 1.50, 6), "4" : Item(4, "Orange Juice", 1.00, 5) } def select_option(self): print(" ") print("Welcome to My Vending Machine") print ("{} Display Products".format(options[0])) print ("{} Choose Product".format(options[1])) print ("{} Transactions".format(options[2])) option = input("Please select option: ") if option == options[0]: VendingMachine.display_items(self) VendingMachine.select_option(self) elif option == options[1]: VendingMachine.order_product(self) elif option == options[2]: VendingMachine.display_items(self) elif option not in (options): print("This option is not recongised") VendingMachine.select_option(self) def display_items(self): print (" ") print ("All items are currently stocked and available") print ("Display items here") def order_product(self): choice = (input("Please insert the item code: ")) product = self.items[choice] print (f"You have selected {product.name} for ${product.price}. Currently {product.amount} in stock") coin = float(input("Please insert your coins: ")) if float(coin) not in (accepted_coins): print ("The Vending Machine accepts only {}" . format(accepted_coins), end = ' ') else: self.total += coin print (f" You have {self.total} in the Machine. You need ${product.price} for {product.name}") print("[1] - Confirm") print("[2] - Change Order") print("[2] - Cancel") confirm = float(input('Currently there is a total of ${:.2f} in the Machine. Confirm Order?: '.format(self.total))) if float(confirm) == 1: self.total -= Items.price print(f'Please take your {product.name} for a total of ${product.price}. Currently there is a total of ${self.total} left in the Vending Machine') elif float(confirm) == 2: print ("Please select again") self.total = 0.00 VendingMachine.select_option(self) elif float(confirm) == 3: print("Please take your money, and have a nice day!") self.total = 0.00 VendingMachine.order_product(self) elif float(confirm) < 3: print("Incorrect Answer - Please take your money and try again") self.total = 0.00 VendingMachine.order_product(self) def main(): vending_machine = VendingMachine() vending_machine.select_option() return 0 if __name__ == "__main__": import sys sys.exit(main())