初学Python:银行金额数字转大写汉字
list_chinese
= ['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖', '拾']
list_unit
= ['圆', '拾', '佰', '仟', '萬']
price
= input('input 金额 五位以下: ')
price
= int(price
[:5]) # 去首
0
list_price
= list(str(price
))
end_zero
= 1 # 末尾是否为
0
flag
= 1 # 当前是否为
0
len_price
= len(list_price
)
for i in
range(len_price
):
list_price
[i
] = list_chinese
[int(list_price
[i
])] # 转大写
zero
= list_chinese
[0] # 零
if list_price
[-1] == zero
:
end_zero
= 0
for i in
range(len_price
):
if i
== len_price
- 1 and end_zero
== 0:
print(list_unit
[0], end
='')
break
elif i
== len_price
- 1 and end_zero
== 1:
print(list_price
[i
], end
='')
print(list_unit
[len_price
- i
- 1], end
='')
else:
if list_price
[i
] == zero
:
flag
= 0 # 当前为
0
else:
flag
= 1
if flag
== 1 or
(flag
== 0 and list_price
[i
- 1] != zero and end_zero
== 1):
print(list_price
[i
], end
='')
if flag
== 1 and i
!= len_price
- 1: # 若当前不为
0
print(list_unit
[len_price
- i
- 1], end
='')
print('整')
End.
转载请注明原文地址:https://blackberry.8miu.com/read-2443.html