Number to Kanji

Hello,

I’ve built this number to kanji converter (adopted Chinese characters in Japanese), but it feels… bulky.

Is there an easier way to build this - maybe without the lengthy list?

I was thinking about converting the integers to string, assign kanji to values in every position in the string, add other necessary characters (tens, hundreds, etc), but then I got confused with handling zeros, so I ended up sticking to this code for now.

Any input is welcome (:

import random

def numtokanji(num):
    kanji = ["一", "二", "三", "四", "五", "六", "七", "八", "九", "十",
             
             "二十", "三十", "四十", "五十", "六十", "七十", "八十", "九十", "百", 
             
             "二百", "三百", "四百", "五百", "六百", "七百", "八百", "九百", "千", 
             
             "二千", "三千", "四千", "五千", "六千", "七千", "八千", "九千", "一万", 
             
             "二万", "三万", "四万", "五万", "六万", "七万", "八万", "九万", "十万", 
             
             "十一万", "十二万", "十三万", "十四万", "十五万", "十六万", "十七万", "十八万", "十九万",
             
             "二十万", "二十一万", "二十二万", "二十三万", "二十四万", "二十五万", "二十六万", "二十七万", "二十八万", "二十九万", 
             
             "三十万", "三十一万", "三十二万", "三十三万", "三十四万", "三十五万", "三十六万", "三十七万", "三十八万", "三十九万",
             
             "四十万", "四十一万", "四十二万", "四十三万", "四十四万", "四十五万", "四十六万", "四十七万", "四十八万", "四十九万",
             
             "五十万", "五十一万", "五十二万", "五十三万", "五十四万", "五十五万", "五十六万", "五十七万", "五十八万", "五十九万",
             
             "六十万", "六十一万", "六十二万", "六十三万", "六十四万", "六十五万", "六十六万", "六十七万", "六十八万", "六十九万",
             
             "七十万", "七十一万", "七十二万", "七十三万", "七十四万", "七十五万", "七十六万", "七十七万", "七十八万", "七十九万",
             
             "八十万", "八十一万", "八十二万", "八十三万", "八十四万", "八十五万", "八十六万", "八十七万", "八十八万", "八十九万",
             
             "九十万", "九十一万", "九十二万", "九十三万", "九十四万", "九十五万", "九十六万", "九十七万", "九十八万", "九十九万",
             
             "百万", 
             
             "二百万", "三百万", "四百万", "五百万", "六百万", "七百万", "八百万", "九百万", "千万"
             
             ]
    
    value = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 
             
             20, 30, 40, 50, 60, 70, 80, 90, 100, 
             
             200, 300, 400, 500, 600, 700, 800, 900, 1000, 
             
             2000, 3000, 4000, 5000, 6000, 7000, 8000, 9000, 10000, 
             
             20000, 30000, 40000, 50000, 60000, 70000, 80000, 90000, 100000, 
             
             110000, 120000, 130000, 140000, 150000, 160000, 170000, 180000, 190000,
             
             200000, 210000, 220000, 230000, 240000, 250000, 260000, 270000, 280000, 290000,
             
             300000, 310000, 320000, 330000, 340000, 350000, 360000, 370000, 380000, 390000,
             
             400000, 410000, 420000, 430000, 440000, 450000, 460000, 470000, 480000, 490000,
             
             500000, 510000, 520000, 530000, 540000, 550000, 560000, 570000, 580000, 590000,
             
             600000, 610000, 620000, 630000, 640000, 650000, 660000, 670000, 680000, 690000,
             
             700000, 710000, 720000, 730000, 740000, 750000, 760000, 770000, 780000, 790000,
             
             800000, 810000, 820000, 830000, 840000, 850000, 860000, 870000, 880000, 890000,
             
             900000, 910000, 920000, 930000, 940000, 950000, 960000, 970000, 980000, 990000,
             
             1000000, 
             
             2000000, 3000000, 4000000, 5000000, 6000000, 7000000, 8000000, 9000000, 10000000 
             
            ]
    
    #empty string to add kanji to
    kanjinum = ""
    
    # reverse lists
    list.reverse(kanji)
    list.reverse(value)
    
    i = 0
    while num > 0:
        for x in range(num // value[i]):
            kanjinum += kanji[i]
            num -= value[i]
        i += 1
    return kanjinum

random1 = random.randint(1, 100)
random2 = random.randint(100, 10000)
random3 = random.randint(10000, 100000)
random4 = random.randint(100000, 10000000)


print(str(random1) + "\t\t" + str(numtokanji(random1)))
print(str(random2) + "\t\t" + str(numtokanji(random2)))
print(str(random3) + "\t\t" + str(numtokanji(random3)))
print(str(random4) + "\t\t" + str(numtokanji(random4)))

print(numtokanji(100904))

Welcome to the forums!

Using a dictionary might be better in this case. If you haven’t learnt about dictionaries yet, you can find the documentation on it here.

Additionally, you might want to refactor your code that obtains the correct Kanji number. Note that you never actually declared num as a variable before using it in the condition of the while loop. Additionally, looping over your lists so many times is going to impact your runtime and make your program very slow, especially since your lists are already very large or if you extend your lists to include more numbers.

1 Like