0

hope someone can help. im trying to create a virtual vending machine. ive got so far with my code but stuck on a few things. i need help in creating a code in this section.

count = 0
TotalCredit = 0
coinNum = int (input("How many coins would you like to enter: "))
while count in range (coinNum):
    coin = float (input ("Coins accepted 0.10 0.20 0.50 1.00: £ "))
    TotalCredit = TotalCredit + coin
count = count + 1

so if coins entered are anything other than 0.10 0.20 0.50 1.00 it prints a message "Invalid coin entered please try again" and loops back to start.

i also need a while loop so if there is not enough credit entered it prints "insufficient funds please add more credit" and goes back to allow you to add credit. I know the minimum amount of credit is 0.59 so i have and idea the the loop is something like 'while TotalCredit <0.59 but not sure how to send user back to add more. ive listed code below so you can see how far ive gone. im only 15 and just learning coding so please as much help as possible would be much appreciated.

def vendingMachine():

    count = 0
    TotalCredit = 0
    coinNum = int (input("How many coins would you like to enter: "))
    while count in range (coinNum):
        coin = float (input ("Coins accepted 0.10 0.20 0.50 1.00: £ "))
        TotalCredit = TotalCredit + coin
    count = count + 1

    print ("You have £",round(TotalCredit,2),"credit " )    
    print ("")
    print ("Choose your item:")
    print ("")       
    print ("1.Coco-Cola")
    print ("2.Walkers Cheese & Onion")
    print ("3.Lucozade")
    print ("4.Wosits")
    print ("5.Water")
    print ("")
    FinalCredit = TotalCredit
    round (FinalCredit, 2)

    item = int (input ("Enter the number for your item: "))
    print ("")
    while item <1 or item >5:
        print ("This item is not available.")
        item = int (input ("Enter the number for your item: "))
    if item == 1:
        FinalCredit = TotalCredit - 0.59
        print ("You now have a Coca-Cola can, costing £0.59.")
        print ("You have",round(FinalCredit,2),"credit remaning.") 
    elif item == 2:
        FinalCredit = TotalCredit - 0.69
        print ("You now have a Walkers crisp packet, costing £0.69.")
        print ("You have", round(FinalCredit,2),"credit remaning.")  
    elif item == 3:
        FinalCredit = TotalCredit - 0.99 
        print ("You now have a Lucozade drink, costing £0.99.")
        print ("You have" ,round(FinalCredit,2),"credit remaning.") 
    elif item == 4:
        FinalCredit = TotalCredit - 0.59
        print ("You now have a Wosits crisp packet, costing £0.59.")
        print ("You have",round(FinalCredit,2),"credit remaning.")   
    elif item == 5:
        FinalCredit = TotalCredit - 0.79
        print ("You now have a bottle of water, costing £0.79.")
        print ("You have",round(FinalCredit,2),"credit remaning.") 
    else:
        print ("This not an option.")
        print ("")
        print ("The rest of your money, £(0), has been        
    outputted.".format(round(FinalCredit,2)))       

vendingMachine ()
hmir
  • 1,333
  • 2
  • 17
  • 24
  • 1
    Please fix the indentation. For the *coin input* portion - have a look at this SO Q&A [Asking the user for input until they give a valid response](http://stackoverflow.com/q/23294658/2823755) – wwii Mar 22 '15 at 15:14
  • A good practice is to write down, in words, what you are trying to do and the steps needed to accomplish this (much like you did when asking this question). Take a look at that *specification* and try to group it into *functional* chunks. Try turning that text into code - play around in the shell to see how things work. – wwii Mar 22 '15 at 15:30
  • 1
    If you are having trouble imagining how to turn your *pseudocode* into code, it may mean you don't have enough *vocabulary* yet and need to spend more time with a [tutorial](https://docs.python.org/3/tutorial/index.html), [BeginnersGuideNonProgrammers](https://wiki.python.org/moin/BeginnersGuide/NonProgrammers) – wwii Mar 22 '15 at 15:30

1 Answers1

0

How about putting the second part of your code in a different method, and then calling vendingMachine() based on user input? Also, to stop the balance from going below 0, we can add an extra conditional statement in the if/elif chain. We'll also need to add TotalCredit parameter (explained below). I added some other edits, which I'll later go into detail with, as well.

def query(TotalCredit):
    print ("You have £" + str(round(TotalCredit,2)) + " credit\n")    
    print ("Choose your item:\n\n1.Coco-Cola\n2.Walkers Cheese & Onion\n3.Lucozade\n4.Wosits\n5.Water\n")
    FinalCredit = TotalCredit
    round (FinalCredit, 2)

    item = int (input ("Enter the number for your item: "))
    print ("")
    while item <1 or item >5:
        print ("This item is not available.")
        item = int (input ("Enter the number for your item: "))
    if item == 1 and FinalCredit >= 0.59:
        FinalCredit = TotalCredit - 0.59
        print ("You now have a Coca-Cola can, costing £0.59.")
    elif item == 2 and FinalCredit >= 0.69:
        FinalCredit = TotalCredit - 0.69
        print ("You now have a Walkers crisp packet, costing £0.69.") 
    elif item == 3 and FinalCredit >= 0.99:
        FinalCredit = TotalCredit - 0.99 
        print ("You now have a Lucozade drink, costing £0.99.") 
    elif item == 4 and FinalCredit >= 0.59:
        FinalCredit = TotalCredit - 0.59
        print ("You now have a Wosits crisp packet, costing £0.59.")
    elif item == 5 and FinalCredit >= 0.79:
        FinalCredit = TotalCredit - 0.79
        print ("You now have a bottle of water, costing £0.79.")
    else:
        print ("This not an option.\n")
        print ("The rest of your money, £(0), has been outputted.".format(round(FinalCredit,2)))
   print ("You have "+str(round(FinalCredit,2))+" credit remaning.") 
   again = input("Would you like to enter the vending machine (y/n)?")
   while again != 'y' and again != 'n':
       again = input("Please input y or n")
   if again == 'y':
       vendingMachine()

Next, in vendingMachine(), all we need to do is make a call to query() and pass TotalCredit.

def vendingMachine():
    TotalCredit = 0
    count = 0
    coinNum = int (input("How many coins would you like to enter: "))
    while count in range (coinNum):
        coin = float (input ("Coins accepted 0.10 0.20 0.50 1.00: £ "))
        TotalCredit = TotalCredit + coin
        count = count + 1
    query(TotalCredit)

Now, the program will continue to run until to user enters 'n' when prompted. Here's the full code:

def query(TotalCredit):
    print ("You have £" + str(round(TotalCredit,2)) + " credit\n")     
    print ("Choose your item:\n\n1.Coco-Cola\n2.Walkers Cheese & Onion\n3.Lucozade\n4.Wosits\n5.Water\n")
    FinalCredit = TotalCredit
    round (FinalCredit, 2)

    item = int (input ("Enter the number for your item:\n"))
    while item <1 or item >5:
        print ("This item is not available.")
        item = int (input ("Enter the number for your item: "))
    if item == 1 and FinalCredit >= 0.59:
        FinalCredit = TotalCredit - 0.59
        print ("You now have a Coca-Cola can, costing £0.59.")
    elif item == 2 and FinalCredit >= 0.69:
        FinalCredit = TotalCredit - 0.69
        print ("You now have a Walkers crisp packet, costing £0.69.")
    elif item == 3 and FinalCredit >= 0.99:
        FinalCredit = TotalCredit - 0.99 
        print ("You now have a Lucozade drink, costing £0.99.")
    elif item == 4 and FinalCredit >= 0.59:
        FinalCredit = TotalCredit - 0.59
        print ("You now have a Wosits crisp packet, costing £0.59.")
    elif item == 5 and FinalCredit >= 0.79:
        FinalCredit = TotalCredit - 0.79
        print ("You now have a bottle of water, costing £0.79.")
    else:
        print ("This not an option.\n")
        print ("The rest of your money, £(0), has been outputted.".format(round(FinalCredit,2)))
    print ("You have " + str(round(FinalCredit,2)) + " credit remaning.") 
    again = input("Would you like to enter the vending machine (y/n)?\n")
    while again != 'y' and again != 'n':
       again = input("Please input y or n\n")
    if again == 'y':
       vendingMachine()


def vendingMachine():
    count = 0
    TotalCredit = 0
    coinNum = int (input("How many coins would you like to enter: "))
    while count in range (coinNum):
        coin = float (input ("Coins accepted 0.10 0.20 0.50 1.00: £ "))
        TotalCredit = TotalCredit + coin
        count = count + 1
    query(TotalCredit)

vendingMachine()

Some other things:

  • You can put \n inside of a string to add a new line, instead of using an emtpy print statement
  • str converts a type into a string, and a + between strings concatenates, them or brings them together.
  • raw_input is the same as input, except it returns a string (only in Python 2.7)
  • If you find yourself continually repeating the same line(s) of code, then you either need to put it in a function or find a better place for it. For example, you used print ("You have",round(FinalCredit,2),"credit remaning.") after every if/elif statement. Instead, you could've just added it at the end (which I did).

Good luck on learning programming! Let me know if you have any other questions. In the future, make sure to format your code properly before posting. I'd also take @wwii's advice on pseudocode and planning out what you're going to write beforehand.

hmir
  • 1,333
  • 2
  • 17
  • 24
  • go it to but comes up this. Traceback (most recent call last): File "C:/Users/Darren/Desktop/101.py", line 47, in vendingMachine() File "C:/Users/Darren/Desktop/101.py", line 45, in vendingMachine query(TotalCredit) File "C:/Users/Darren/Desktop/101.py", line 30, in query again = raw_input("Would you like to enter the vending machine (y/n)?\n") NameError: name 'raw_input' is not defined – Darren Mills Mar 22 '15 at 19:15
  • Seems like you're using Python 3. I changed all the raw-input() references to input(). It should work now – hmir Mar 22 '15 at 23:21