Posts: 9
Threads: 3
Joined: Apr 2020
Hi, I have a project to build a vending machine that must display items and balance of coins within the machine. I have done some work on it and am stuck on line 31 of my code. I have defined self.total however it is coming back as an invalid syntax? I am happy for you to critique my code however I am completely new to this, and I am trying to keep it simple. I have only completed the code for one item so far in my machine
Thanks in advance!
class Vending_Machine(object):
aussie_coins = (0.05, 0.10, 0.20, 0.50, 1.00, 2.00)
items = ['Tea','Coffee', 'Coke', 'Orange Juice']
item_price = [0.50, 1.00, 1.50, 1.00]
item_code = [1, 2, 3, 4]
def __init__(self):
self.total = 0.00
def insert_coin(self,coin):
if float(coin) not in (self.aussie_coins):
print ('The Vending Machine accepts only: {}. ' .format(self.aussie_coins), end = '')
else:
self.total += coin
print ('Currently there is a total of {: .2f} in machine' .format(self.total))
def menu():
print("Welcome to my Vending Machine")
print(item_code[0], items[0], item_price[0]) #using the dict. from vending machine class
print(item_code[1], items[1], item_price[1])
print(item_code[2], items[2], item_price[2])
print(item_code[3], items[3], item_price[3])
def user_input(self, choice):
choice = input("Please enter the item code of an item you would like to purchase: ")
if choice == item_code[0]:
print ("You have selected {} - the price is ${.2f}. Currently you have a total of ${.2f} in the machine." .format(items[0], item_price[0], self.total))
while self.total < item_price[0]: #if not the price of tea then..
insert_coin(input("Insert a coin into the vending machine: "))
else self.total >= item_price[0]:
self.total -= item_price[0]
print('Please take your {}. Total of {:.2f} in the machine' .format(items[0],self.total))
Posts: 1,838
Threads: 2
Joined: Apr 2017
You don't have conditions in else statements (line 30). Think about it - "else" means "in the other case", i.e that the preceding condition was False .
Posts: 9
Threads: 3
Joined: Apr 2020
Thanks, I went through and adjusted it - seems to have sorted that issues.. I am a little further through, and have got it working until I enter my coin into the machine.
I am not sure how to pull a method from my super class? I think I have everything I need to insert into the section but am unable to run that when the user inserts the coin value. It just gives me a value and doesn't call the method? Any help or comments to put me in the right direction will be appreciated!!
class Vending_Machine:
aussie_coins = (0.05, 0.10, 0.20, 0.50, 1.00, 2.00)
items = ['Tea','Coffee', 'Coke', 'Orange Juice']
item_price = [0.50, 1.00, 1.50, 1.00]
item_code = ['1', '2', '3', '4']
def __init__(self):
self.total = 0.00
def insert_coin(self,coin):
if float(coin) not in (self.aussie_coins):
print ('The Vending Machine accepts only: {}. ' .format(self.aussie_coins), end = '')
else:
self.total += coin
print ('Currently there is a total of {: .2f} in machine' .format(self.total))
class interface(Vending_Machine):
def menu(self):
print("##################################")
print(" Welcome to My Vending Machine ")
print("All items below are readily available")
print(Vending_Machine.item_code[0], Vending_Machine.items[0], Vending_Machine.item_price[0]) #
print(Vending_Machine.item_code[1], Vending_Machine.items[1], Vending_Machine.item_price[1])
print(Vending_Machine.item_code[2], Vending_Machine.items[2], Vending_Machine.item_price[2])
print(Vending_Machine.item_code[3], Vending_Machine.items[3], Vending_Machine.item_price[3])
print("##################################")
class user_input(interface):
def choice (self):
choice = input("Please enter the item code of an item you would like to purchase: ")
if choice == Vending_Machine.item_code[0]:
print ("You have selected {} - the price is ${:.2f}. Currently you have a total of ${:.2f} in the machine." .format(Vending_Machine.items[0], Vending_Machine.item_price[0], self.total))
if self.total < Vending_Machine.item_price[0]: #if not the price of tea then..
Vending_Machine.insert_coin = float(input("Insert a coin into the vending machine"))
elif self.total > Vending_Machine.item_price[0]:
self.total -= Vending_Machine.item_price[0]
print('Please take your {}. Total of {:.2f} in the machine' .format(Vending_Machine.items[0],self.total))
elif self.total == Vending_Machine.item_price[0]:
self.total -= Vending_Machine.item_price[0]
print('Please take your {}. Thanks, have a nice day!'.format(Vending_Machine.items[0]))
elif choice == Vending_Machine.item_code[1]:
print ("You have selected {} - the price is ${:.2f}. Currently you have a total of ${:.2f} in the machine." .format(Vending_Machine.items[1], Vending_Machine.item_price[1], self.total))
if self.total < Vending_Machine.item_price[1]: #if not the price of tea then..
insert_coin(input("Insert a coin into the vending machine: "))
elif self.total > Vending_Machine.item_price[1]:
self.total -= Vending_Machine.item_price[1]
print('Please take your {}. Total of {:.2f} in the machine' .format(Vending_Machine.items[1],self.total))
elif self.total == Vending_Machine.item_price[1]:
self.total -= Vending_Machine.item_price[1]
print('Please take your {}. Thanks, have a nice day!'.format(Vending_Machine.items[1]))
elif choice == Vending_Machine.item_code[2]:
print ("You have selected {} - the price is ${.2f}. Currently you have a total of ${.2f} in the machine." .format(Vending_Machine.items[2], Vending_Machine.item_price[2], self.total))
if self.total < Vending_Machine.item_price[1]: #if not the price of tea then..
insert_coin(input("Insert a coin into the vending machine: "))
elif self.total > Vending_Machine.item_price[2]:
self.total -= Vending_Machine.item_price[2]
print('Please take your {}. Total of {:.2f} in the machine' .format(Vending_Machine.items[1],self.total))
elif self.total == Vending_Machine.item_price[2]:
self.total -= Vending_Machine.item_price[2]
print('Please take your {}. Thanks, have a nice day!'.format(Vending_Machine.items[2]))
elif choice == Vending_Machine.item_code[3]:
print ("You have selected {} - the price is ${.2f}. Currently you have a total of ${.2f} in the machine." .format(Vending_Machine.items[3], Vending_Machine.item_price[3], self.total))
if self.total < Vending_Machine.item_price[3]: #if not the price of tea then..
insert_coin(input("Insert a coin into the vending machine: "))
elif self.total > Vending_Machine.item_price[3]:
self.total -= Vending_Machine.item_price[3]
print('Please take your {}. Total of {:.2f} in the machine' .format(Vending_Machine.items[3],self.total))
elif self.total == Vending_Machine.item_price[3]:
self.total -= Vending_Machine.item_price[3]
print('Please take your {}. Thanks, have a nice day!'.format(Vending_Machine.items[3]))
elif choice not in item_code:
print("Sorry we do not have item number {} available. Please try again" .format(choice))
vm = Vending_Machine()
i1 = interface()
u1 = user_input()
i1.menu()
u1.choice()
Posts: 1,838
Threads: 2
Joined: Apr 2017
(Apr-03-2020, 05:18 AM)wiggles Wrote: I am not sure how to pull a method from my super class? I think I have everything I need to insert into the section but am unable to run that when the user inserts the coin value. It just gives me a value and doesn't call the method? Any help or comments to put me in the right direction will be appreciated!!
I don't really understand what you mean. Can you provide an example (i.e. sample input and output) and a pointer to which lines of code aren't working as you expect?
Posts: 9
Threads: 3
Joined: Apr 2020
Sure, so I can get my code to run for the project up until it asks to insert a coin (lines 33 - 36). I was trying to pull the method that was Vending_Machine.insert_coin in the Vending Machine class. I think it pulls correctly, as I get the right String output. However, I can't seem to get it to follow on with the rest of the user_input class? It just seems to stop?
Thanks Wiggles
#creating a dictionary to outline all the drinks in the machine and their price
class Vending_Machine:
aussie_coins = (0.05, 0.10, 0.20, 0.50, 1.00, 2.00)
items = ['Tea','Coffee', 'Coke', 'Orange Juice']
item_price = [0.50, 1.00, 1.50, 1.00]
item_code = ['1', '2', '3', '4']
def __init__(self): #define total in vending machine.
self.total = 0.00
def insert_coin(self,coin):
if float(coin) not in (self.aussie_coins):
print ('The Vending Machine accepts only: {}. ' .format(self.aussie_coins), end = '')
else:
self.total += coin
print ('Currently there is a total of ${:.2f} in machine' .format(self.total))
class interface(Vending_Machine):
def menu(self):
print("##################################")
print(" Welcome to Tom's Vending Machine ")
print("All items below are readily available")
print(Vending_Machine.item_code[0], Vending_Machine.items[0], "${: .2f}".format(Vending_Machine.item_price[0])) #
print(Vending_Machine.item_code[1], Vending_Machine.items[1], "${: .2f}".format(Vending_Machine.item_price[1]))
print(Vending_Machine.item_code[2], Vending_Machine.items[2], "${: .2f}".format(Vending_Machine.item_price[2]))
print(Vending_Machine.item_code[3], Vending_Machine.items[3], "${: .2f}".format(Vending_Machine.item_price[3]))
print("##################################")
class user_input(interface):
def choice (self):
choice = input("Please enter the item code of an item you would like to purchase: ")
if choice == Vending_Machine.item_code[0]:
print ("You have selected {} - the price is ${: .2f}. Currently you have a total of ${: .2f} in the machine." .format(Vending_Machine.items[0], Vending_Machine.item_price[0], self.total))
if self.total < Vending_Machine.item_price[0]:
coins = float(input("Insert a coin into the vending machine: "))
Vending_Machine.insert_coin(self,coins)
elif self.total > Vending_Machine.item_price[0]:
self.total -= Vending_Machine.item_price[0]
print('Please take your {}. Total of {: .2f} in the machine' .format(Vending_Machine.items[0],self.total))
elif self.total == Vending_Machine.item_price[0]:
self.total -= Vending_Machine.item_price[0]
print('Please take your {}. Thanks, have a nice day!'.format(Vending_Machine.items[0]))
elif choice == Vending_Machine.item_code[1]:
print ("You have selected {} - the price is ${: .2f}. Currently you have a total of ${: .2f} in the machine." .format(Vending_Machine.items[1], Vending_Machine.item_price[1], self.total))
if self.total < Vending_Machine.item_price[1]:
insert_coin(input("Insert a coin into the vending machine: "))
elif self.total > Vending_Machine.item_price[1]:
self.total -= Vending_Machine.item_price[1]
print('Please take your {}. Total of {: .2f} in the machine' .format(Vending_Machine.items[1],self.total))
elif self.total == Vending_Machine.item_price[1]:
self.total -= Vending_Machine.item_price[1]
print('Please take your {}. Thanks, have a nice day!'.format(Vending_Machine.items[1]))
elif choice == Vending_Machine.item_code[2]:
print ("You have selected {} - the price is ${: .2f}. Currently you have a total of ${: .2f} in the machine." .format(Vending_Machine.items[2], Vending_Machine.item_price[2], self.total))
if self.total < Vending_Machine.item_price[2]:
insert_coin(input("Insert a coin into the vending machine: "))
elif self.total > Vending_Machine.item_price[2]:
self.total -= Vending_Machine.item_price[2]
print('Please take your {}. Total of {: .2f} in the machine' .format(Vending_Machine.items[2],self.total))
elif self.total == Vending_Machine.item_price[2]:
self.total -= Vending_Machine.item_price[2]
print('Please take your {}. Thanks, have a nice day!'.format(Vending_Machine.items[2]))
elif choice == Vending_Machine.item_code[3]:
print ("You have selected {} - the price is ${: .2f}. Currently you have a total of ${: .2f} in the machine." .format(Vending_Machine.items[3], Vending_Machine.item_price[3], self.total))
if self.total < Vending_Machine.item_price[3]: #if not the price of tea then..
insert_coin(input("Insert a coin into the vending machine: "))
elif self.total > Vending_Machine.item_price[3]:
self.total -= Vending_Machine.item_price[3]
print('Please take your {}. Total of {: .2f} in the machine' .format(Vending_Machine.items[3],self.total))
elif self.total == Vending_Machine.item_price[3]:
self.total -= Vending_Machine.item_price[3]
print('Please take your {}. Thanks, have a nice day!'.format(Vending_Machine.items[3]))
elif choice not in item_code:
print("Sorry we do not have item number {} available. Please try again" .format(choice))
vm = Vending_Machine()
i1 = interface()
u1 = user_input()
i1.menu()
u1.choice()
Posts: 9
Threads: 3
Joined: Apr 2020
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())
|