stuck in a problem in making a program to number of currency notes

Question:

I have an assignment to count currency notes of 1000,500,100,50,20 and 10,i tried many thing for loop and if condition but could not get the correct output.please guide me me the correct code and explanation.i want the output like
input=Enter amount:1070
output=There are ‘1’ 1000 note,’0′ 500 note,’0′ 100 notes,’1′ 50 note,’1′ 20 note,’0′ 10 note in given amount.
simply output should be 1 note of 1000,1 note of 50,1 note of 20.

    while True:
                while True:
                            try:
                                amount = int(input('nEnter First number:'))
                            except ValueError:
                                print("nPlease enter only number")
                            else:
                                break
                if (amount>=1000):
                    n_1000=amount/1000
                if (amount>=500):
                    n_500=amount/500
                if (amount>=100):
                    n_100=amount/100
                if (amount>=50):
                    n_50=amount/50
                if (amount>=20):
                    n_20=amount/20
                if (amount>=10):
                    n_10=amount/10
                print("nThere are {0} 1000 notes,{1} 500 notes,{2} 100 
               notes,{3} 50 notes,{4} 20 notes,{5} 10 notes in given 
               amount.".format(n_1000,n_500,n_100,n_50,n_20,n_10))
                while True:
                            Repeat=input("nDo you want to repeat?nnYes or No:")
                            Repeat=Repeat.lower()
                            if Repeat not in ["yes","y","no","n"]:
                                print("nPlease select correct option")
                            else:
                                break


                if Repeat in ["yes","y"]:
                    continue
                else:
                    if Repeat in ["no","n"]:
                        print("n-----Thank you for using-----")
                        input()
                        break
Asked By: waseem12312

||

Answers:

I’ve ran your code, in python division of ints can produce a float, so you need to use the // operator (which returns an int). You also might want to change the amount, so to not return the same change from every note, that is for 1000, your current code will return “there are 1 1000 note, 2 500 notes” etc, you can change every if statement to

if (amount >= 1000):
    n_1000 = amount // 1000
    amount %= 1000

And it would be alot easier if you will keep a list of notes, and iterate through it

Answered By: Rotem Tal

I think this would be a better solution to your task:

END_COMMAND = 'quit'

def func():
    while True:
        while True:
            amount = input('Enter an integer amount (or type "{}"):'.format(END_COMMAND))
                .lower().strip()
            if amount == END_COMMAND:
                print('Received "{}" instruction'.format(END_COMMAND))
                return

            try:
                amount = int(amount)
                break
            except ValueError:
                print('Error: "{}" is not a valid integer'.format(amount))

        print('Composition for amount "{:,d}":'.format(amount))
        for denomination in [1000, 500, 100, 50, 20, 10, 1]:
            n, amount = divmod(amount, denomination)

            if n > 0:
                print('   {:6,d} of denomination {:6,d}'.format(n, denomination))


if __name__ == '__main__':
    func()
Answered By: Ralf

The correct code is this,no need for condition or loop.

    while True:
                while True:
                            try:
                                amount = int(input('nEnter First number:'))
                            except ValueError:
                                print("nPlease enter only number")
                            else:
                                break
                n_1000=amount//1000
                amount=amount-(n_1000*1000)
                n_500=amount//500
                amount=amount-(n_500*500)
                n_100=amount//100
                amount=amount-(n_100*100)
                n_50=amount//50
                amount=amount-(n_50*50)
                n_20=amount//20
                amount=amount-(n_20*20)
                n_10=amount//10
                amount=amount-(n_10*10)
                l_amount=amount
                print("nThere are {0} 1000 notes,{1} 500 notes,{2} 100 notes,{3} 50 notes,{4} 20 notes,{5} 10 notes and {6} is left in given amount.".format(n_1000,n_500,n_100,n_50,n_20,n_10,l_amount))
                while True:
                            Repeat=input("nDo you want to repeat?nnYes or No:")
                            Repeat=Repeat.lower()
                            if Repeat not in ["yes","y","no","n"]:
                                print("nPlease select correct option")
                            else:
                                break


                if Repeat in ["yes","y"]:
                    continue
                else:
                    if Repeat in ["no","n"]:
                        print("n-----Thank you for using-----")
                        input()
                        break

        if Repeat in ["yes","y"]:
            continue
        else:
            if Repeat in ["no","n"]:
                print("n-----Thank you for using-----")
                input()
                break
Answered By: Waseem Munir

This is the simplest solution to your question

n = int(input("Enter an amount"))
    twothousands=int((n/2000))
    fivehundred=int((n%2000) / 500)
    hundreds=int((n %500)/100)
    fifties =int((n%100)/50)
    tens=int((n%500)%50/10)
    ones=int(((n%500)%50)%10)
    print("2000:{} 500:{} 100:{} 50:{} 10:{} 1:{}".format(twothousands,fivehundred,hundreds,fifties,tens,ones))

#python

Answered By: Mooaz Sayyed
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.