python - Vending Machine Program (Calculate the amount that has to inserted,etc.) -


i want write program simulate vending machine , calculate change (which has returned you) based on amount paid. given cost, user should first prompted add more money until cost met/exceeded payment.

assume change given in coins , coins come in following denominations: 1c, 5c, 10c, 25c, $1

here program:

 x = eval(input("enter cost (in cents):\n"))  b = 0   in range(x+500):             if x<5 , x>=b:         b += 1         print("deposit coin or note (in cents):")         print(1)         diff = b-x         in range(diff):             onecents = diff//1             new_onecents = diff - (onecents*1)             print("your change is:")             if onecents != 0:                print(onecents,"x 1c")                               break          elif x<10 , x>=b:         b += 5         print("deposit coin or note (in cents):")         print(5)         diff = b-x         in range(diff):             fivecents = diff//5             new_fivecents = diff - (fivecents*5)             onecents = new_fivecents//1             new_onecents = new_fivecents - (onecents*1)             print("your change is:")             if fivecents != 0:                 print(fivecents,"x 5c")             if onecents != 0:                 print(onecents,"x 1c")                                       break            elif x<25 , x>=b:         b += 10         print("deposit coin or note (in cents):")         print(10)         diff = b-x         in range(diff):             tencents = diff//10             new_tencents = diff - (tencents*10)             fivecents = new_tencents//5             new_fivecents = new_tencents - (fivecents*5)             onecents = new_fivecents//1             new_onecents = new_fivecents - (onecents*1)             print("your change is:")             if tencents !=0:                 print(tencents,"x 10c")             if fivecents != 0:                 print(fivecents,"x 5c")                                     if onecents != 0:                 print(onecents,"x 1c")                                   break              elif x<100 , x>=b:         b += 25         print("deposit coin or note (in cents):")         print(25)         diff= b-x         in range(diff):             quarters = diff//25             new_quarters = diff - (quarters*25)             tencents = new_quarters//10             new_tencents = new_quarters - (tencents*10)             fivecents = new_tencents//5             new_fivecents = new_tencents - (fivecents*5)             onecents = new_fivecents//1             new_onecents = new_fivecents - (onecents*1)             print("your change is:")             if quarters !=0:                 print(quarters,"x 25c")                  if tencents !=0:                 print(tencents,"x 10c")                                 if fivecents != 0:                 print(fivecents,"x 5c")                                 if onecents != 0:                 print(onecents,"x 1c")                               break      elif x<500 , x>b:         print("deposit coin or note (in cents):")         print(100)         b += 100         diff = b-x         in range(diff):             quarters = diff//25             new_quarters = diff - (quarters*25)             tencents = new_quarters//10             new_tencents = new_quarters - (tencents*10)             fivecents = new_tencents//5             new_fivecents = new_tencents - (fivecents*5)             onecents = new_fivecents//1             new_onecents = new_fivecents - (onecents*1)             print("your change is:")             if quarters !=0:                 print(quarters,"x 25c")             if tencents !=0:                 print(tencents,"x 10c")                     if fivecents != 0:                 print(fivecents,"x 5c")                         if onecents != 0:                 print(onecents,"x 1c")             break                   elif x<(x+500) , x>=b:         print("deposit coin or note (in cents):")         print(500)         b += 500         diff = b-x         in range(diff):             onedollars = diff//100             new_onedollars = diff - (onedollars * 100)             quarters = new_onedollars//25             new_quarters = new_onedollars - (quarters*25)             tencents = new_quarters//10             new_tencents = new_quarters - (tencents*10)             fivecents = new_tencents//5             new_fivecents = new_tencents - (fivecents*5)             onecents = new_fivecents//1             new_onecents = new_fivecents - (onecents*1)             print("your change is:")             if onedollars != 0:                 print(onedollars,"x $1")             if quarters !=0:                 print(quarters,"x 25c")             if tencents !=0:                 print(tencents,"x 10c")                     if fivecents != 0:                 print(fivecents,"x 5c")                         if onecents != 0:                 print(onecents,"x 1c")             break  

when run program , follow instructions should this:

enter cost (in cents): 1000 deposit coin or note (in cents): 500 deposit coin or note (in cents): 500 deposit coin or note (in cents): 

instead get:

enter cost (in cents): 1000 deposit coin or note (in cents): 500 deposit coin or note (in cents): 500 deposit coin or note (in cents): 500 change is: 5 x $1 

also expected output:

enter cost (in cents): 3 deposit coin or note (in cents): 1 deposit coin or note (in cents): 1 deposit coin or note (in cents): 1 

however get:

enter cost (in cents): 3 deposit coin or note (in cents): 1  deposit coin or note (in cents): 1 deposit coin or note (in cents): 1 deposit coin or note (in cents): 1 change is: 1 x 1c 

the rest work out , correct.

thanks guys (especially @jonrsharpe). here solution (in code form):

def vend():     """simulate vending machine, taking user input , returning remainder."""     total = eval(input("enter cost (in cents):\n"))     inserted = 0     while inserted < total:         inserted += eval(input("deposit coin or note (in cents):\n"))     if inserted > total:         sum = inserted - total         if sum != 0:             print("your change is:")         dollars = sum//100         if dollars != 0:             print(dollars,'x $1')         quarters = (sum - dollars*100)//25         if quarters != 0:             print(quarters,'x 25c')         ten_cents = (sum - dollars*100 - quarters*25)//10         if ten_cents != 0:             print(ten_cents,'x 10c')         five_cents = (sum - dollars*100 - quarters*25 - ten_cents*10)//5         if five_cents != 0:             print(five_cents,'x 5c')         one_cents = (sum - dollars*100 - quarters*25 - ten_cents*10 - five_cents*5)//1         if one_cents != 0:             print(one_cents,'x 1c')  vend() 

your specific error stems fact not deal correctly case reach total - overshoot, have give change. however, code long , complex , difficult figure out doing @ each stage.

a few general coding suggestions:

  1. eval bad idea; better use int(input(...)), make fuss if user doesn't enter integer.
  2. your outer for loop should while loop, rather guessing maximum number of iterations (which run, after you've made change!)
  3. you have a lot of repeated code , hard-coded values; whenever write similar things repeatedly consider splitting them out function arguments, or kind of loop.

also, reading description, specifically:

given cost, user should first prompted add more money until cost met/exceeded payment.

i think supposed allowing user input coins rather guessing enter.

here 1 possible implementation:

def vend():     """simulate vending machine, taking user input , returning remainder."""     total = int(input("enter cost (in cents): "))     inserted = 0     while inserted < total:         inserted += int(input("deposit coin or note (in cents): "))     if inserted > total:         return make_change(inserted - total)  def make_change(remainder):     """calculate coins required make change equal amount."""     coins = ["$1", "25c", "10c", "5c", "1c"]     amounts = [int(coin[:-1]) if coin.endswith("c")                 else 100 * int(coin[1:])                 coin in coins]     counts = [0 _ in coins]     index, amount in enumerate(amounts):         counts[index] = remainder // amount         remainder %= amount     return ", ".join("{0} x {1}".format(count, coin)                       count, coin in zip(counts, coins)                       if count) 

note division of responsibility between 2 functions, make easier test each 1 separately, , appropriate use of for , while loops minimise repetition. also, have made amounts , paid in make_change depend on coins, have change in 1 place add new coins.

example usage:

>>> vend() enter cost (in cents): 135 deposit coin or note (in cents): 100 deposit coin or note (in cents): 50 '1 x 10c, 1 x 5c' 

Comments

Popular posts from this blog

windows - Single EXE to Install Python Standalone Executable for Easy Distribution -

c# - Access objects in UserControl from MainWindow in WPF -

javascript - How to name a jQuery function to make a browser's back button work? -