user5956712
user5956712

Reputation:

Can't reset index for loop in Python

I want to create a program which can be used for an ATM. This is what I've made:

# 500, 20, 100, 50, 10, 5, 1
nrb = 0
i=0
j=0
bancnote = [500, 200, 100, 50, 10, 5, 1]
for i in range(7):
    print('b:',bancnote[i])
suma = int(input('Scrieti suma dorita: '))
while suma > 0:
    while j <= 6:
        if suma >= bancnote[j]:
            nrb +=1
            suma -=bancnote[j]
            print('Am scazut: ', bancnote[j])
            print('Ramas: ',suma)
            print("Bancnote: ",nrb)
            j=0

My counter for that loop can't be reset. What can I do?

Upvotes: 0

Views: 809

Answers (3)

guidot
guidot

Reputation: 5333

Your indices are not really required by Python. Considering what I understood from the problem, the code should look similar to this:

note_values = (500, 200, 100, 50, 10, 5, 1)

def partition(value):
    result = []
    for note in note_values:
        whole, value = divmod(value, note)
        result.append(whole)
    return result

if __name__ == "__main__":
    value = int(input("Sum wanted: "))
    notes = partition(value)
    for number, value in zip(notes, note_values):
        if number != 0:
            print("{} note of {:3d}".format(number, value))

Upvotes: 0

Mohideen bin Mohammed
Mohideen bin Mohammed

Reputation: 20147

there is no increment in variable j . so the loop will remain as it is

Upvotes: 1

Mihai-Daniel Virna
Mihai-Daniel Virna

Reputation: 994

(It is easier for me to understand the code as I can read the language as well)

What you forgot is to increment j, so your code will only look at the 500 bank note every time. Thus not trying to decrease other value from the sum.

Upvotes: 1

Related Questions