Apr-04-2020, 05:28 AM
Righto - so I have gone about this a little different. I have changed up my classes and have the following code with two classes instead of 3. My issue is with lines 36 - 39. I need to reference the input above and depending on y/n input either start the method again, or move onto the next method below (order_product)
The second issue is that I am having trouble in my method - order_product, with calling the other parts of the class item (i.e. user inputs a float value that aligns with self.code, I want to confirm they are selecting the right item by displaying the self.name and self.price..
Any feedback would be greatly appreciated!
The second issue is that I am having trouble in my method - order_product, with calling the other parts of the class item (i.e. user inputs a float value that aligns with self.code, I want to confirm they are selecting the right item by displaying the self.name and self.price..
Any feedback would be greatly appreciated!
accepted_coins= [0.05, 0.10, 0.20, 0.50, 1.00, 2.00] class Item: def __init__(self, code, name, price): self.code = code self.name = name self.price = price class VendingMachine: def __init__(self): self.items = [ Item(1, "Tea", 0.50), Item(2, "Coffee", 1.00), Item(3, "Coke", 1.50), Item(4, "Orange Juice", 1.00) ] def display_items(self): print("Welcome to my Vending Machine") for code, item in enumerate(self.items, start=1): print(f"[{code}] - {item.name} (${item.price:.2f})") def insert_coin(self): self.total = 0.00 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 input('Currently there is a total of ${:.2f} in the Machine. Continue to order? y/n' .format(self.total)) if INPUT ABOVE == Y or y: order_product() elif i == N or n: RUN THIS METHOD AGAIN() def order_product(self): choice = float(input("Please insert the item code: ")) if float(choice) == self.code(): print ("You have selected {} for $(:.2f)".format(item.self, item.price)) def main(): vending_machine = VendingMachine() vending_machine.display_items() vending_machine.insert_coin() return 0 if __name__ == "__main__": import sys sys.exit(main())