Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Vending machine help
#6
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!

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


Messages In This Thread
Vending machine help - by wiggles - Apr-03-2020, 01:09 AM
RE: Vending machine help - by ndc85430 - Apr-03-2020, 03:50 AM
RE: Vending machine help - by wiggles - Apr-03-2020, 05:18 AM
RE: Vending machine help - by ndc85430 - Apr-03-2020, 07:14 AM
RE: Vending machine help - by wiggles - Apr-03-2020, 07:49 AM
RE: Vending machine help - by wiggles - Apr-04-2020, 05:28 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Vending Machine Project - OOP Issues wiggles 2 3,528 Apr-05-2020, 05:15 AM
Last Post: wiggles

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020