Python练习3(阿拉伯数字串转换为中文数字串)

题目:
编写代码,尽可能好的实现阿拉伯数字串到中文数字串的转换,例如139->一百三十九,10086->一万零八十六

import math
a = int(input("请输入阿拉伯数字串:"))
b = len(str(a)) #字符串位数
number = ['零','一','二','三','四','五','六','七','八','九']
unit = ['零','十','百','千','万','亿']
list = [] #储存中文数字串
while(b>0):
    c = a//pow(10,b-1)
    if(c != 0):
        list.append(number[c])
    if(c == 0 and len(list)>1 and list[-1] != unit[0]):
        list.append(number[c])
    d = a%pow(10,b-1)
    e = (b-1)%4
    if(e > 0 and c != 0):
        list.append(unit[e])
    f = b//4
    if(f == 1 and e == 0 and list[-1] != unit[5]):
        list.append(unit[4])
    elif(f == 2 and e == 0):
        list.append(unit[5])
    a = d
    b = b-1
i = 0
while( i < len(list)):
    if(i == 0): 
        if(list[i] == number[1] and list[i+1] == unit[1]):
            print(list[i+1],end = '')
            i = i + 2
    if(i != len(list) - 1 and list[i] == unit[0] and (list[i-1] == unit[1] or list[i-1] == unit[2] or list[i-1] == unit[3] or list[i-1] == unit[4]) and (list[i+1] == unit[4] or list[i+1] == unit[5])):
        print(list[i+1], end = '')
        i = i + 2
    if (i == len(list) - 1 and list[i] == unit[0]):
        break
    else:
        print(list[i], end = '')
        i = i + 1

你可能感兴趣的:(python)