Hamge

学习记录

导航

Python 找零问题

#coding = utf-8

def Change_Money(money):
    print('总金额:'+str(money)+'元')
    
    loop=True
    tmp=[]

    # 面值列表 单位:元
    type = [100,50,20,10,5,1,0.5,0.1]

    sy = int(money*10)  #将传入的金额*10,转换为'角'单位

    while loop:
        if sy == 0:         #循环判断
            loop=False
        else:
            for row in type:
                tmpStr = ''
                coin = int(row * 10)  #将纸币面额*10,转换为'角'单位

                if coin >= 10:    #判断币额为什么单位
                    unit = '元'
                else:
                    unit = '角'

                if sy >= coin and tmpStr == '':
                    count = sy // coin   #相除求出有多少张币
                    sy = sy % coin   #求余看剩下多少金额
                    if coin>=10:
                        tmpStr = str(coin//10) + unit + str(count)+'张'
                    else:
                        tmpStr = str(coin) + unit+str(count) + '张'
                    tmp.append(tmpStr)

    return tmp

if __name__ == '__main__':

    a=Change_Money(422.5)  #传入金额
    for x in a:
        print (x)

  

posted on 2019-04-25 15:26  Hamge  阅读(755)  评论(0编辑  收藏  举报