【发布时间】:2021-05-13 19:02:29
【问题描述】:
我有一个程序,目前需要两个数字并将它们转换为分数的单词, 例如,“frac(2,3) 应该返回“三分之二”,但我需要第二个输入的帮助,将其转换为单词“第三”。现在像 frac (2,3) 这样的东西只会返回“二" 但我需要它返回 "三分之二"
我的程序:
def frac (numer, den):
top = {'1': "one", '2': "two", '3': "three", '4': "four", '5': "five", '6': "six",
'7': "seven", '8': "eight", '9': "nine", '0': "zero"}
bottom = {'2': "half", '3': "third", '4':"Fourth",'5':"Fifth",'6':"sixth",'7':"seventh",'8':"eighth",'9':"ninth",'10':"tenth"}
return " ".join(map(lambda x: top[x], str(numer)))
【问题讨论】:
-
试过使用 num2words 库吗? github.com/savoirfairelinux/num2words