制定程序来解决纸币数量问题

时间:2019-06-29 12:30:41

标签: python

我有一个作业要对1000,500,100,50,20和10的纸币进行计数,我尝试了许多循环操作,如果条件正常但无法获得正确的输出。请指导我正确的代码和解释.i想要输出像 输入=输入金额:1070 输出=在给定的数量下有'1'1000笔记,'0'500笔记,'0'100笔记,'1'50笔记,'1'20笔记,'0'10笔记。 简单地输出应该是1000的1便笺的50,1的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?\n\nYes 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

3 个答案:

答案 0 :(得分:0)

我已经运行了您的代码,在python中,int的除法会产生一个浮点数,因此您需要使用//运算符(返回一个int)。您可能还想更改金额,以便不从每个便笺中返回相同的更改,即1000,当前代码将返回“有1 1000个便笺,2 500个便笺”等,您可以将每个if语句更改为

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

如果您保留一个笔记列表并进行遍历,会容易得多

答案 1 :(得分:0)

我认为这将是对您的任务更好的解决方案:

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()

答案 2 :(得分:0)

正确的代码是这个,不需要条件或循环。

    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?\n\nYes 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