数字到Python中的单词

时间:2016-02-15 18:35:44

标签: python

我想要编写一个接受1到9999(含)之间的整数的程序,然后用单词打印该值。例如,如果用户输入:

  

3421

然后程序应该打印:

three thousand four hundred twenty one

我的代码如下,但它不起作用。

num=int(input('please enter an integer between 1 and 9999: '))

def int_to_en(num):
    d = { 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 : 'ninteen', 20 : 'twenty', \
          30 : 'thirty', 40 : 'fourth', 50 : 'fifty', 60 : 'sixty', \
          70 : 'seventy', 80 : 'eighty', 90 : 'ninty' }
    k = 1000
    m = k * 1000

    if (num < 20):
        print(d[num])

    if (num < 100):
        if num % 10 == 0:
            print(d[num])
        else:
            print(d[num // 10 * 10] + '' + d[num % 10])

    if (num < k):
        if num % 100 == 0:
            print(d[num // 100] + ' hundred')
        else:
            print(d[num // 100] + ' hundred ' + int_to_en(num % 100))

    if (num < m):
        if num % k == 0:
            print(int_to_en(num // k) + ' thousand')
        else:
            print(int_to_en(num // k) + ' thousand, ' + int_to_en(num % k))

3 个答案:

答案 0 :(得分:2)

之前我创建了这样一个函数。它并不完美,但效果很好:

/[()]|\d+(,\d+)?|\{[+*\/\-]\}/

用法示例:

def word_form(number):
    """word_form(number) -> string

    Returns the word form of the number.
    >>> word_form(32)
    'thirty-two'
    >>> word_form(123)
    'one hundred twenty-three'
    The highest number it is capable of converting is:
        999,999,999,999,999,999,999,999,999,999,999"""

    ones = ("", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine")
    tens = ("", "", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety")
    teens = ("ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen")
    levels = ("", "thousand", "million", "billion", "trillion", "quadrillion", "quintillion", "sextillion", "septillion", "octillion", "nonillion")

    word = ""
    #number will now be the reverse of the string form of itself.
    num = reversed(str(number))
    number = ""
    for x in num:
        number += x
    del num
    if len(number) % 3 == 1: number += "0"
    x = 0
    for digit in number:
        if x % 3 == 0:
            word = levels[x / 3] + ", " + word
            n = int(digit)
        elif x % 3 == 1:
            if digit == "1":
                num = teens[n]
            else:
                num = tens[int(digit)]
                if n:
                    if num:
                        num += "-" + ones[n]
                    else:
                        num = ones[n]
            word = num + " " + word
        elif x % 3 == 2:
            if digit != "0":
                word = ones[int(digit)] + " hundred " + word
        x += 1
    return word.strip(", ")

答案 1 :(得分:2)

int_to_en的返回值为None,这将使代码中的连接失败,尝试返回String并将结果连接起来。

num=int(input('please enter an integer between 1 and 9999: '))

def int_to_en(num):
    d = { 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 : 'ninteen', 20 : 'twenty', \
          30 : 'thirty', 40 : 'fourth', 50 : 'fifty', 60 : 'sixty', \
          70 : 'seventy', 80 : 'eighty', 90 : 'ninty' }
    k = 1000
    m = k * 1000

    if (num < 20):
        return d[num]

    if (num < 100):
        if num % 10 == 0:
            return d[num]
        else:
            return d[num // 10 * 10] + ' ' + d[num % 10]

    if (num < k):
        if num % 100 == 0:
            return d[num // 100] + ' hundred'
        else:
            return d[num // 100] + ' hundred ' + int_to_en(num % 100)
    if (num < m):
        if num % k == 0:
            return int_to_en(num // k) + ' thousand'
        else:
            return int_to_en(num // k) + ' thousand, ' + int_to_en(num % k)

print int_to_en(num)

答案 2 :(得分:0)

一旦你打扰调用该函数,你遇到的下一个问题就是 if 语句不是独占的。例如,输入只有 5 ...     你看到5&lt; 20。     你打印“五”。     你转到下一个if语句。果然,5&lt; 100。     您将数字处理为更大的...并进入无限递归。

您必须确保只执行那些 if 语句的一个。除了第一个之外的所有内容,请使用 elif 而不是 if

要超越问题,您可以将打印语句适当地更改为返回语句。在你回到主程序之前不要打印任何东西;其他调用只是在你弄清楚时附加结果,主程序立即获取整个字符串。

为了节省很多时间,请了解basic debugging