数字到单词翻译器占零

时间:2018-10-22 02:26:51

标签: python conditional-statements

我正在创建一个数字到单词的转换器,并使其具有一个例外。

例如,如果输出4056中键入了数字'four thousand zero hundred fifty six',我就无法计算零。此外,如果在50中键入'fifty zero',则会输出。

到目前为止,我所包含的内容如下:

def convert(number_str):

    d1 = {0: 'zero',1: 'one', 2: 'two', 3: 'three', 4: 'four', 5: 'five', \
         6: 'six', 7: 'seven', 8: 'eight', 9: 'nine', 10: 'ten', \
         11: 'eleven', 12: 'twelve', 13: 'thirteen', 14: 'fourteen', \
         15: 'fifteen', 16: 'sixteen', 17: 'seventeen', 18: 'eighteen', 19: 'nineteen'}
    l2 = ['twenty','thirty','fourty','fifty','sixty','seventy','eighty','ninety']

    if (int(number_str) >= 1) and (int(number_str) < 19):
        return(d1[number_str])
    elif (int(number_str) >= 20) and (int(number_str) <99):
        tens, single = divmod(number_str, 10)
        return l2[tens-2] + " " + d1[single]
    elif (int(number_str) >= 100) and (int(number_str) <999):
        hundreds, tens1 = divmod(number_str, 100)
        tens, single = divmod(tens1,10)
        return(d1[hundreds]+' hundred '+l2[tens-2]+" "+d1[single])
    elif (int(number_str) >= 1000) and (int(number_str) <9999):
        thousands, hundreds1 = divmod(number_str,1000)
        hundreds, tens1 = divmod(hundreds1,100)
        tens, single = divmod(tens1,10)
        return(d1[thousands]+' thousand '+d1[hundreds]+' hundred '+l2[tens-2]+" "+d1[single])

def main():

    user_input = input('> ')
    while  user_input != 'quit':
        print(convert(int(user_input)))
        user_input = input('> ')

main()

我可以添加什么逻辑以更好地处理使用零来支持该程序?预先感谢!

2 个答案:

答案 0 :(得分:1)

您需要为零添加特殊条件。例如,可以使用if语句检查50的问题:

lif (int(number_str) >= 20) and (int(number_str) <99):
    tens, single = divmod(number_str, 10)
    if single == 0:
        return l2[tens-2]
    else:
        return l2[tens-2] + " " + d1[single]

如果要在数百个位置处理0,则可以编写类似的内容。

答案 1 :(得分:-1)

我们使用的语言编号系统在使用有效地使用零作为占位符的阿拉伯数字编号系统之前就得到了很好的发展。

我想一些没有注释任何零的逻辑将起作用。纯粹将其视为占位符,以使左侧的数字具有更高的10 ^ n值。

def convert(number_str):

    d1 = {0: '',1: 'one', 2: 'two', 3: 'three', 4: 'four', 5: 'five', \
         6: 'six', 7: 'seven', 8: 'eight', 9: 'nine', 10: 'ten', \
         11: 'eleven', 12: 'twelve', 13: 'thirteen', 14: 'fourteen', \
         15: 'fifteen', 16: 'sixteen', 17: 'seventeen', 18: 'eighteen', 19: 'nineteen'} 
 # Changed zero to an empty string
    l2 = ['twenty','thirty','fourty','fifty','sixty','seventy','eighty','ninety']
    if int(number_str) == 0: # Catches the unique case when zero is mentioned
      return('zero')
    elif (int(number_str) >= 1) and (int(number_str) < 19):
        return(d1[number_str])
    elif (int(number_str) >= 20) and (int(number_str) <=99):
        tens, single = divmod(number_str, 10)
        return l2[tens-2] + " " + d1[single]
    elif (int(number_str) >= 100) and (int(number_str) <=999):
        hundreds, tens1 = divmod(number_str, 100)
        tens, single = divmod(tens1,10)
        return(d1[hundreds]+' hundred '+((l2[tens-2]+" "+d1[single]) if tens > 1 else d1[tens*10+single]))
        # Added a conditional statement above to deal with numbers ending
        # in a number less than 20
    elif (int(number_str) >= 1000) and (int(number_str) <=9999):
        thousands, hundreds = divmod(number_str,1000)
        return(d1[thousands]+' thousand '+ convert(hundreds))
        # Added a recursive call to save code

def main():

    user_input = input('> ')
    while  user_input != 'quit':
        print(convert(int(user_input)))
        user_input = input('> ')

main()

请注意,它在两个位置进行了编辑。首先,将零设为空字符串,然后捕获零的情况。