1

I am trying to make a simple vending machine program this is my first time writing an independent program in python on my own. I am not posting the whole code in the program unless requested. My issue is that the program will keep displaying infinite messages, basically an infinite loop. I will post a small portion of the program here, again if needed I can post the full program.

def vending_process(item):
    balance=20
    vending_items={'Haribo':20 , 'Hershey':21, 'Lays':22, 'Cheetos':23, 'Pepsi':24, 'Dew':25, 'Rockstar':26, 'Monster': 27, 'Starbucks':28, 'Nescafe':29}
    item=int(input("Choose your item from the vending machine! Choose -1 to abort: "))
    if item==-1:
        exit(0)
    else:
      while item!=-1: 
        if item not in vending_items.values():
           print("No such entry!")
           continue
        elif item in vending_items.values():
             if item==20:
               balance=balance-1.5
               print('You bought Haribo for 1.50! your balance is {}'.format(balance))

My issue ere is that the Print message will keep repeating itself. I want it to print back the "Choose your item from the vending machine!" and repeat the process while still updating the balance.

Jos
  • 127
  • 6
  • 2
    Does this answer your question? [Asking the user for input until they give a valid response](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) – quamrana Jan 30 '21 at 11:30
  • 1
    The `input` should be inside the while loop as should be also the question – piertoni Jan 30 '21 at 11:32
  • Hi! Thank you for the response, this does not really answer my question as the program does not crash, but rather it just goes in an infinite loop. – Jos Jan 30 '21 at 11:32
  • You are missing to read item input again – Wonka Jan 30 '21 at 11:32
  • My issue is that if I put the input inside the while loop it will give me an error that item is not defined. How can I fix that? – Jos Jan 30 '21 at 11:33
  • Just define it before with a value, let's say = 0 – piertoni Jan 30 '21 at 11:34

4 Answers4

2

Some suggestions: I have added comments in the code where necessary. This may be improved upon, but a few points: no need to pass item as it is asked for through user input, use return for exit, no need to run elif or similar conditionals as the routine will leave via return before it reaches the rest of the code:

def vending_process(): # no need to pass 'item' as this is an inpt
    balance=20
    vending_items={'Haribo':20 , 'Hershey':21, 'Lays':22, 'Cheetos':23, 'Pepsi':24, 'Dew':25, 'Rockstar':26, 'Monster': 27, 'Starbucks':28, 'Nescafe':29}
    
    while balance > 0: # continue process till balance drops below 0
        item=int(input("Choose your item from the vending machine! Choose -1 to abort: "))
        if item==-1:
            return -1 # exit (return preferable)
        if item not in vending_items.values():
            print("No such entry!")
            return -2 # invalid item
        if item == 20:
            balance -= 1.5
            print('You bought Haribo for 1.50! your balance is {}'.format(balance))
                
vending_process()

This produces

Choose your item from the vending machine! Choose -1 to abort: 20
You bought Haribo for 1.50! your balance is 18.5
Choose your item from the vending machine! Choose -1 to abort: 20
You bought Haribo for 1.50! your balance is 17.0
Choose your item from the vending machine! Choose -1 to abort: 20
You bought Haribo for 1.50! your balance is 15.5
Choose your item from the vending machine! Choose -1 to abort: 20
You bought Haribo for 1.50! your balance is 14.0
Choose your item from the vending machine! Choose -1 to abort: 20
You bought Haribo for 1.50! your balance is 12.5
Choose your item from the vending machine! Choose -1 to abort: 20
You bought Haribo for 1.50! your balance is 11.0
Choose your item from the vending machine! Choose -1 to abort: 20
You bought Haribo for 1.50! your balance is 9.5
Choose your item from the vending machine! Choose -1 to abort: 20
You bought Haribo for 1.50! your balance is 8.0
Choose your item from the vending machine! Choose -1 to abort: 20
You bought Haribo for 1.50! your balance is 6.5
Choose your item from the vending machine! Choose -1 to abort: 20
You bought Haribo for 1.50! your balance is 5.0
Choose your item from the vending machine! Choose -1 to abort: 20
You bought Haribo for 1.50! your balance is 3.5
Choose your item from the vending machine! Choose -1 to abort: 20
You bought Haribo for 1.50! your balance is 2.0
Choose your item from the vending machine! Choose -1 to abort: 20
You bought Haribo for 1.50! your balance is 0.5
Choose your item from the vending machine! Choose -1 to abort: 20
You bought Haribo for 1.50! your balance is -1.0

Next steps would be to add handling other items, maybe then passing the balance and the items dict to the function to handle increasingly general cases.

GrimTrigger
  • 571
  • 1
  • 5
  • 10
1

Try this:

while item!=-1: 
        item = input('Choose your item from the vending machine: ')
        item = int(item)
        if item == -1:
           break
        elif item not in vending_items.values():
           print("No such entry!")
           continue
        elif item in vending_items.values():
             if item==20:
               balance=balance-1.5
               print('You bought Haribo for 1.50! your balance is {}'.format(balance))

Just initialize the value of item to anything other than -1 before the while loop and you're good to go.

The issue is that the value of item never changes in your while loop that you have written. Ideally, you should take user input inside the while loop which will allow for the input to be changing each time you run the loop.

Also a small suggestion - try adding some code to check if the balance goes lower than 0. All the best for your endeavors.

  • 1
    About the balance I plan to add some restrictions in the code later on in the code. But other than that, thank you for the help! Appreciate it. – Jos Jan 30 '21 at 11:40
1
def vending_process(item):
    balance=20
    vending_items={'Haribo':20 , 'Hershey':21, 'Lays':22, 'Cheetos':23, 'Pepsi':24, 'Dew':25, 'Rockstar':26, 'Monster': 27, 'Starbucks':28, 'Nescafe':29}
    item = 0
    while item != -1:
        item=int(input("Choose your item from the vending machine! Choose -1 to abort: ")) 
        if item not in vending_items.values():
           print("No such entry!")
        elif item in vending_items.values():
             if item==20:
                 balance=balance-1.5
                 print('You bought Haribo for 1.50! your balance is {}'.format(balance))
piertoni
  • 1,933
  • 1
  • 18
  • 30
1

Without a broader look at your code. I cannot see the reason for the while item != -1 being used right after you checked that item!=-1which is the reason for your infinite loop.

As I understand you want the user to select an item until they decide to quit. That should be something like:

def vending_process(item):
    balance=20
    vending_items={'Haribo':20 , 'Hershey':21, 'Lays':22, 'Cheetos':23, 'Pepsi':24, 'Dew':25, 'Rockstar':26, 'Monster': 27, 'Starbucks':28, 'Nescafe':29}
    item = 0 #Just to get it started
    while item!=-1:
        item=int(input("Choose your item from the vending machine! Choose -1 to abort: "))
        if item==-1:
            exit(0)
        elif item not in vending_items.values():
           print("No such entry!")
           continue
        elif item in vending_items.values():
             if item==20:
               balance=balance-1.5
               print('You bought Haribo for 1.50! your balance is {}'.format(balance))