Python function: Find Change from purchase amount

前端 未结 6 887
旧时难觅i
旧时难觅i 2021-01-29 13:56

I\'m looking for the most efficient way to figure out a change amount (Quarters, dimes, nickels, and pennies) from a purchase amount. The purchase amount must be less than $1, a

6条回答
  •  耶瑟儿~
    2021-01-29 14:18

    I have an improve solution from above solutions

    coins=[]
    cost = float(input('Input the cost: '))
    give = float(input('Tipe Amount given: '))
    
    change = (give - cost)
    change2 = change-int(change)
    change2 = round(change2,2)*100
    coin = [50,25,10,5,1]
    for c in coin:
        while change2>=c:
            coins.append(c)
            change2 = change2-c
        
    half=coins.count(50)
    qua = coins.count(25)
    dime=coins.count(10)
    ni=coins.count(5)
    pen=coins.count(1)
    dolars = int(change)
    if half !=0 or qua != 0 or dime!=0 or ni!=0 or pen!=0:
        print ('The change of', round(give,2), 'is:',change, 'like \n-dolas:',dolars,'\n-halfs:',half,'\n-quarters:',qua,'\n-dime:',dime,'\n-nickels:',ni,'\n-pennies:',pen)
    else:
         print ('The change from', round(give,2), 'is:',change,'and no coins')
    

提交回复
热议问题