改变算法" Ten One" num2text中的十一个?

时间:2017-03-19 14:23:08

标签: python algorithm python-2.7

我编写了一个将数字翻译成文本的代码:

num = raw_input("Insert a number > ")
numbers = {'0': '', '1': 'One ', '2': 'Two ', '3': 'Three ', '4': 'Four ',
           '5': 'Five ', '6': 'Six ', '7': 'Seven ', '8': 'Eight ', '9': 'Nine '}
tens = {'0': '', '1': 'Ten ', '2': 'Twenty ', '3': 'Thirty ', '4': 'Forty ',
        '5': 'Fifty ', '6': 'Sixty ', '7': 'Seventy ', '8': 'Eighty ', '9': 'Ninety '}
numbers_teens = {'0': '','1': 'Eleven ', '2': 'Twelve ', '3': 'Thirteen ',
                 '4': 'Fourteen ', '5': 'Fifteen ', '6': 'Sixteen ', '7': 'Seventeen ',
                 '8': 'Eighteen ', '9': 'Nineteen '}
numbers_teeens = {'0' : '', '1': 'One', '2': 'Two', '3': 'Three', '4': 'Four',
                  '5': 'Five', '6': 'Six', '7': 'Seven', '8': 'Eight', '9': 'Nine',
                  '11': 'Eleven', '12': 'Twelve', '13': 'Thirteen', '14': 'Fourteen',
                  '15': 'Fifteen', '16': 'Sixteen', '17': 'Seventeen', '18': 'Eighteen',
                  '19': 'Nineteen'}
numbers_hundreds = {}
numbers_thousands = {}
numbers_ten_thousands = {}
numbers_teen_thousands = {}
numbers_hundred_thousands = {}

#Creates dicts
for k,v in numbers.items():
    numbers_hundreds.update({k: v.title() + 'Hundred '})
if k == 0:
    numbers_hundreds.update({k: v.title() + ''})
if '0' in numbers_thousands:
    numbers_thousands.update({k: v.title() + ''})
numbers_thousands.update({k: v.title() + 'Thousand, '})

for k1,v1 in tens.items():
    numbers_ten_thousands.update({k1: v1.title()})

for kt,vt in numbers_teens.items():
    numbers_teen_thousands.update({kt: vt.title() + 'Thousand '})
if kt == 0:
    numbers_teen_thousands.update({kt: vt.title() + ''})

for k2,v2 in numbers_hundreds.items():
    numbers_hundred_thousands.update({k2: v2.title() + ''})

p = len(num)
i = p - 2
if p == 1:
    print numbers[num]
if p == 2:
    print tens[num[0]] + numbers_teeens[num[1]]

if p == 3:
    print numbers_hundreds[num[0]] + tens[num[1]] + numbers_teeens[num[2]]

if p == 4:
    print (numbers_thousands[num[0]] + numbers_hundreds[num[1]] + tens[num[2]]
           + numbers_teeens[num[3]])

if p == 5:
    print (numbers_ten_thousands[num[0]] + numbers_thousands[num[1]]
           + numbers_hundreds[num[2]] + tens[num[3]] + numbers_teeens[num[4]])

if p == 6:
    print (numbers_hundred_thousands[num[0]] + numbers_ten_thousands[num[1]]
           + numbers_thousands[num[2]]+ numbers_hundreds[num[3]] + tens[num[4]]
           + numbers_teeens[num[5]])

但是当我的输入为112时,我的输出为One Hundred Ten Two,(同样适用于所有青少年,{11,12,13,14,15,16,17,18,19})。它应该是One Hundred Eleven,但我不知道该怎么做......

2 个答案:

答案 0 :(得分:0)

试试这个:

num = raw_input("Insert a number > ")

tens = {'0': '', '1': 'Ten ', '2': 'Twenty ', '3': 'Thirty ', '4': 'Forty ',
        '5': 'Fifty ', '6': 'Sixty ', '7': 'Seventy ', '8': 'Eighty ', '9': 'Ninety '}
numbers_teens = {'0': '','1': 'Eleven ', '2': 'Twelve ', '3': 'Thirteen ',
                 '4': 'Fourteen ', '5': 'Fifteen ', '6': 'Sixteen ', '7': 'Seventeen ',
                 '8': 'Eighteen ', '9': 'Nineteen '}
numbers_teeens = {'0' : '', '1': 'One ', '2': 'Two ', '3': 'Three ', '4': 'Four ',
                  '5': 'Five ', '6': 'Six ', '7': 'Seven ', '8': 'Eight ', '9': 'Nine ',
                  '11': 'Eleven ', '12': 'Twelve ', '13': 'Thirteen ', '14': 'Fourteen ',
                  '15': 'Fifteen ', '16': 'Sixteen ', '17': 'Seventeen ', '18': 'Eighteen ',
                  '19': 'Nineteen '}


def two_numbers(num):
    if int(num) < 20:
        out_ans = numbers_teeens[num]
    else:
        out_ans = tens[num[0]] + numbers_teeens[num[1]]

    return out_ans

def three_numbers(num):
    out_ans = ''
    if int(num) > 99:
        out_ans += two_numbers(num[0]) + 'Hundred '

    out_ans += two_numbers(num[-2:])

    return out_ans

def num2text(num):
    out_ans = ''
    if int(num) > 999:
        out_ans += three_numbers(num[:-3]) + 'Thousand, '

    out_ans += three_numbers(num[-3:])

    print out_ans
    return out_ans

num2text(num)

仅适用于小于999999的数字。

您可以参考two_numbers()修改您的代码。

答案 1 :(得分:0)

你可以解决这个问题:

  1. 将numbers_teens键更改为11,12,14..etc
  2. 添加条件以检查十位数,如果是1,则print numbers_teens[num[0]+num[1]]
  3. 加号,我不明白为什么你需要另一个numbers_teeens(3 e)

    我应用了这些更改,请尝试以下代码:

    numbers = {'0': '', '1': 'One ', '2': 'Two ', '3': 'Three ', '4': 'Four ',
               '5': 'Five ', '6': 'Six ', '7': 'Seven ', '8': 'Eight ', '9': 'Nine '}
    tens = {'0': '', '1': 'Ten ', '2': 'Twenty ', '3': 'Thirty ', '4': 'Forty ',
            '5': 'Fifty ', '6': 'Sixty ', '7': 'Seventy ', '8': 'Eighty ', '9': 'Ninety '}
    # 1- changed keys in numbers_teens:
    numbers_teens = {'0': '','11': 'Eleven ', '12': 'Twelve ', '13': 'Thirteen ',
                     '14': 'Fourteen ', '15': 'Fifteen ', '16': 'Sixteen ', '17': 'Seventeen ',
                     '18': 'Eighteen ', '19': 'Nineteen '}
    # removed number_teeens from here
    
    #Creates dicts
    for k,v in numbers.items():
        numbers_hundreds.update({k: v.title() + 'Hundred '})
        if k == 0:
            numbers_hundreds.update({k: v.title() + ''})
        if '0' in numbers_thousands:
            numbers_thousands.update({k: v.title() + ''})
        numbers_thousands.update({k: v.title() + 'Thousand, '})
    
    for k1,v1 in tens.items():
        numbers_ten_thousands.update({k1: v1.title()})
    
    for kt,vt in numbers_teens.items():
        numbers_teen_thousands.update({kt: vt.title() + 'Thousand '})
        if kt == 0:
            numbers_teen_thousands.update({kt: vt.title() + ''})
    
    for k2,v2 in numbers_hundreds.items():
        numbers_hundred_thousands.update({k2: v2.title() + ''})
    
    if p == 1:
        print numbers[num]
    if p == 2:
        # 2- condition to check if tens digits == '1'
        if num[0] == '1':
            # print numbers_teens[the concatenation of the two digits]
            print numbers_teens[num[0]+num[1]]
        # if it is not == '1'
        else:
            print tens[num[0]] + numbers[num[1]]
    
    if p == 3:
        # check if tens digits == '1'
        if num[1] == '1':
            print numbers_hundreds[num[0]] + numbers_teens[num[1]+num[2]]
        else:
            print numbers_hundreds[num[0]] + tens[num[1]] + numbers[num[2]]
    
    if p == 4:
        # check if tens digits == '1'
        if num[2] == '1':
            print numbers_thousands[num[0]]+ numbers_hundreds[num[1]] + numbers_teens[num[2]+num[3]]
        else:
            print numbers_thousands[num[0]]+ numbers_hundreds[num[1]] + tens[num[2]] + numbers[num[3]]
    
    if p == 5:
        # check if tens digits == '1'
        if num[3] == '1':
            print numbers_ten_thousands[num[0]] + numbers_thousands[num[1]]+ numbers_hundreds[num[2]] + numbers_teens[num[3]+num[4]]
        else:
            print numbers_ten_thousands[num[0]] + numbers_thousands[num[1]]+ numbers_hundreds[num[2]] + tens[num[3]] + numbers[num[4]]
    
    if p == 6:
        # check if tens digits == '1'
        if num[4] == '1':
            print numbers_hundred_thousands[num[0]] + numbers_ten_thousands[num[1]] + numbers_thousands[num[2]]+ numbers_hundreds[num[3]] + numbers_teens[num[4]+num[5]]
        else:
            print numbers_hundred_thousands[num[0]] + numbers_ten_thousands[num[1]] + numbers_thousands[num[2]]+ numbers_hundreds[num[3]] + tens[num[4]] + numbers[num[5]]
    

    现在当输入= 112
    时 输出为:One Hundred Twelve