如何将数字转换为单词python 3

时间:2018-11-15 10:18:54

标签: python python-3.x for-loop

我在尝试将数字转换为单词时遇到一个问题:当我输入80时崩溃,或者如果我输入100,它会产生“一百二百”。有人可以帮我修复我的代码。

number = input("Enter a value: ")
ones = ["", "one ", "two ","three ", "four", "five ", "six ", "seven ", 
"eight ", "nine "]
teens = ["ten ", "eleven ", "twelve ", "thirteen ", "fourteen ", "fifteen ", 
"sixteen ", "seventeen ", "eighteen ", "nineteen "]
decades = ["", "twenty ", "thirty ", "forty ", "fifty ", "sixty ", "seventy 
", "eighty ", "ninety "]
hundreds = ["", "one hundred ", "two hundred ", "three hundred ", "four 
hundred ", "five hundred", "six hundred ", "seven hundred", "eight hundred", 
"nine hundred"]

word = ""
change = len(number)

while change > 0:
    if number == "0":
        word ="zero"
        break
    elif change > 1 and number[change - 2] == "1":
        for i in range(0,10):
            if number[change - 1] == str(i):
                word = teens[i] + word
    else:
        for i in range(0,10):
            if number[change - 1] == str(i):
                word = ones[i] + word
    if change > 1:
        for i in range(0,10):
            if number[change - 2] == str(i):
                word = decades[i] + word
    if change > 2:
        for i in range(0,10):
            if number[change - 3] == str(i):
                word = hundreds[i] + word
    change = change - 3
print(word)

5 个答案:

答案 0 :(得分:0)

这应该有效:word2number

以及他们的文档中的示例: print(w2n.word_to_num('one hundred thirty-five'))

答案 1 :(得分:0)

几十年来,您错过了“七十”和“八十”之间的逗号。您必须将数十年更改为以下内容:

decades = ["", "twenty ", "thirty ", "forty ", "fifty ", "sixty ", "seventy", "eighty ", "ninety"]

在hunderds列表中也存在相同的错误。使用此新列表:

hundreds = ["", "one hundred ", "two hundred ", "three hundred ", "four hundred ", "five hundred", "six hundred ", "seven hundred", "eight hundred", "nine hundred"]

尽管如此,您的实现还是有很多错误。

答案 2 :(得分:0)

这if语句将起作用(请注意,这是使它起作用所需的全部):

if len(number)==1:
   print(ones[int(number)])
elif len(number)==2 and number[0]=='1':
   print(teens[int(number[-1])])
elif len(number)==2 and number[0]!='1':
   print(decades[int(number[0])-1])
elif len(number)==3:
   print(hundreds[int(number[0])])

请注意,必须在decadeshundreds中添加逗号,因此请复制以下内容,然后替换为剪贴板:

decades = ["", "twenty ", "thirty ", "forty ", "fifty ", "sixty ", "seventy", "eighty ", "ninety "]
hundreds = ["", "one hundred ", "two hundred ", "three hundred ", "four hundred ", "five hundred", "six hundred ", "seven hundred", "eight hundred", 

答案 3 :(得分:0)

我认为,为了使您的示例正常工作,您只需要确保每个change位数字都可以访问列表中的正确值即可。对于您的decades列表,情况并非如此,因为您错过了青少年的空白字符串,因此您正确地将其视为单独的情况。

因此,只需更改代码即可:

decades = ["", "twenty ", "thirty ", "forty ", "fifty ", "sixty ", "seventy", "eighty ", "ninety "]

对此:

decades = ["", "", "twenty ", "thirty ", "forty ", "fifty ", "sixty ", "seventy", "eighty ", "ninety "]

答案 4 :(得分:0)

对于此实现,列表中应始终包含10个元素。例如,几十年来,即使不使用它,也应该有一些字符串表示10-20之间的值,因为否则数组的索引将是错误的。

仍然,此代码有很多错误。 试试这个。

number = input("Enter a value: ")
ones = ["", "one ", "two ","three ", "four", "five ", "six ", "seven ", "eight ", "nine "]
teens = ["ten ", "eleven ", "twelve ", "thirteen ", "fourteen ", "fifteen ", "sixteen ", "seventeen ", "eighteen ", "nineteen "]
decades = ["", "onety", "twenty ", "thirty ", "forty ", "fifty ", "sixty ", "seventy ", "eighty ", "ninety "]
hundreds = ["", "one hundred ", "two hundred ", "three hundred ", "four hundred ", "five hundred", "six hundred ", "seven hundred", "eight hundred", "nine hundred"]

word = ""
change = len(number)

while change > 0:
    if number == "0":
        word ="zero"
        break
    elif change > 1 and number[change - 2] == "1":
        for i in range(0,10):
            if number[change - 1] == str(i):
                word = teens[i] + word
        if change == 2:
            break
    else:
        for i in range(0,10):
            if number[change - 1] == str(i):
                word = ones[i] + word
    if change > 1 and number[change-2]!="1":
        for i in range(0,10):
            if number[change - 2] == str(i):
                word = decades[i] + word
    if change > 2:
        for i in range(0,10):
            if number[change - 3] == str(i):
                word = hundreds[i] + word
    change = change - 3
print(word)