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)
相關文章
相關標籤/搜索