Python implementation of a small change program code sharing
- 2020-04-02 14:01:04
Python wrote a program to get change according to the face value, according to our normal thinking logic from the large to small face value of the change method, the face value of RMB has 100 yuan, 50 yuan, 20 yuan, 10 yuan, 5 yuan, 1 yuan, 5 jiao, 1 jiao, and the program also set these face value accordingly. Simply pass in the amount of change you want when you call the function, and the program automatically calculates how many bills of each denomination should be found. If 50 yuan is passed in, the system will automatically calculate the change of 50 yuan a face value, if 60 yuan 7 cents, the program will automatically calculate the change of 50 yuan a, 10 yuan a, 5 jiao a, 1 jiao two.
# encoding=UTF-8 def zhaoqian(money): loop=True tmp=[' Total amount: '+str(money)+' yuan '] # Face value list Unit: cate=( 100, 50, 20, 10, 5, 1, 0.5, 0.1 ) sy=int(money*10) while loop: if sy==0: loop=False else: for row in cate: tmpStr = '' jine=int(row*10) if jine>=10: tmpUn = ' yuan ' else: tmpUn = ' Angle ' if sy>=jine and tmpStr=='': m = sy//jine sy = sy%jine if jine>=10: tmpStr = str(jine//10) + tmpUn + STR (m) + 'a' else: tmpStr = str(jine)+tmpUn+str(m)+' zhang ' tmp.append(tmpStr) return tmp a=zhaoqian(88.7) for x in a: print x