vendredi 13 août 2021

How to get the correct coin change with given limited coin in vending machine in python

How to design a vending machine that given change with specific how much of change with note and coin ?

if input is 8.5, The aspect change for 8.5 will be RM5:1, RM 1:2, RM 0.5: 3.

The problem that I got is: at the first for loop,I aspect the for loop value for i should stop at 1 when the k < x.and the x value ends at 3.5.

Then,at second for loop value 'j' should be stop at 2 when k < x and the x value ends at 1.5.

import numpy as ny
x=8.5
y= [x]
note1=3
note2=2
note3=3
note4=0
ringgit   = ['RM5', 'RM1', 'RM0.50','RM0.20']
num_notes = [note1,note2,note3,note4]
num_notes_change = [0,0,0,0]
#print(y)
i=0
a=0
k=0
j=0
l=0



#print(num_notes[0])    
if x > 5 :

    if num_notes[0]!=0:
        
        for i in ny.arange(num_notes[0]):
            k=i*5
            #print(k)
            
            #print(i)
            if k<x:
                
                #print(k)
                #print(i)
                x=x-k
                print(x)
                num_notes_change[0]=i
                i=i+1
                
            elif x-k<x:
                break
                
            else:
                print("error")
             
        #x="{:.2f}".format(x)
print(x)
    
print(num_notes_change)
print(num_notes)     

if 5 > x >= 1 :

    if num_notes[1]!=0:
        
        for j in ny.arange(num_notes[1]):
            l=j*1
            #print(k)
            
            #print(i)
            if l<x:
                
                #print(k)
                #print(i)
                x=x-l
                print(x)
                num_notes_change[1]=j
                j=j+1
                
            elif x-k<x:
                break
                
            else:
                print("error")
             
        #x="{:.2f}".format(x)
print(num_notes_change)

Aucun commentaire:

Enregistrer un commentaire