将数字更改为不起作用的单词的代码

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了将数字更改为不起作用的单词的代码相关的知识,希望对你有一定的参考价值。

我有此代码将数字更改为与它们相等的单词,因此将1更改为一个,依此类推。该代码在大多数情况下有效,但是我遇到了无法修复的错误。每当我输入1000000 aka 100万返回100万时,有什么想法吗?我尝试过放置一个单独的if语句,如果输入了1000000,则该语句将打印100万,但是这不起作用

def one(num):  #converts numbers up to 9 into words
    word = ''
    if num == '1':
        word = "One"
    if num == '2':
        word = "Two"
    if num == '3':
        word = "Three"
    if num == '4':
        word = "Four"
    if num == '5':
        word = "Five"
    if num == '6':
        word = "Six"
    if num == '7':
        word = "Seven"
    if num == '8':
        word = "Eight"
    if num == '9':
        word = "Nine"
    word = word.strip()
    return word

def ten(num):  #converts numbers between 10 and 19 into words
    word = ''
    if num[0] == '1':
        if num[1] == '0':
            word = "Ten"
        if num[1] == '1':
            word = "Eleven"
        if num[1] == '2':
            word = "Twelve"
        if num[1] == '3':
            word = "Thirteen"
        if num[1] == '4':
            word = "Fourteen"
        if num[1] == '5':
            word = "Fifteen"
        if num[1] == '6':
            word = "Sixteen"
        if num[1] == '7':
            word = "Seventeen"
        if num[1] == '8':
            word = "Eighteen"
        if num[1] == '9':
            word = "Nineteen"
    else: #converts the 10s into words
        text = once(num[1])
        if num[0] == '2':
            word = "Twenty"
        if num[0] == '3':
            word = "Thirty"
        if num[0] == '4':
            word = "Fourty"
        if num[0] == '5':
            word = "Fifty"
        if num[0] == '6':
            word = "Sixty"
        if num[0] == '7':
            word = "Seventy"
        if num[0] == '8':
            word = "Eighty"
        if num[0] == '9':
            word = "Ninety"
        word = word + " " + text
    word = word.strip()
    return word

def hundred(num):  #converts the hundreds into words
    word = ''
    text = ten(num[1:])
    if num[0] == '1':
        word = "One"
    if num[0] == '2':
        word = "Two"
    if num[0] == '3':
        word = "Three"
    if num[0] == '4':
        word = "Four"
    if num[0] == '5':
        word = "Five"
    if num[0] == '6':
        word = "Six"
    if num[0] == '7':
        word = "Seven"
    if num[0] == '8':
        word = "Eight"
    if num[0] == '9':
        word = "Nine"
    if num[0] != '0':
        word = word + " Hundred "
    word = word + text
    word = word.strip()
    return word

def thousand(num): #converts the thousands into words
    word = ''
    pref = ''
    text = ''
    length = len(num)
    if length == 6:
        text = hundred(num[3:])
        pref = hundred(num[:3])
    if length == 5:
        text = hundred(num[2:])
        pref = ten(num[:2])
    if length == 4:
        text = hundred(num[1:])
        if num[0] == '1':
            word = "One"
        if num[0] == '2':
            word = "Two"
        if num[0] == '3':
            word = "Three"
        if num[0] == '4':
            word = "Four"
        if num[0] == '5':
            word = "Five"
        if num[0] == '6':
            word = "Six"
        if num[0] == '7':
            word = "Seven"
        if num[0] == '8':
            word = "Eight"
        if num[0] == '9':
            word = "Nine"
        word = word + " Thousand "
    word = word + text
    if length == 6 or length == 5:
        word = pref + " Thousand " + word
    word = word.strip()
    return word

def million(num): #converts the millions into words
    word = ''
    pref = ''
    text = ''
    length = len(num)
    if length == 9:
        text = thousand(num[3:])
        pref = hundred(num[:3])
    if length == 8:
        text = thousand(num[2:])
        pref = ten(num[:2])
    if length == 7:
        text = thousand(num[1:])
        if num[0] == '1':
            word = "One"
        if num[0] == '2':
            word = "Two"
        if num[0] == '3':
            word = "Three"
        if num[0] == '4':
            word = "Four"
        if num[0] == '5':
            word = "Five"
        if num[0] == '6':
            word = "Six"
        if num[0] == '7':
            word = "Seven"
        if num[0] == '8':
            word = "Eight"
        if num[0] == '9':
            word = "Nine"
        word = word + " Million "
    word = word + text
    if length == 9 or length == 8:
        word = pref + " Million " + word
    word = word.strip()
    return word



number = int(input()) #asks user for their number
numberWord = str(number) #converts the number inputted from integer to string
leng = len(numberWord) #gets the length of the number
if leng == 1: #depending on how long the number is it uses different functions
    num = one(numberWord)
if leng == 2:
    num = ten(numberWord)
if leng == 3:
    num = hundred(numberWord)
if leng > 3 or leng < 6:
    num = thousand(numberWord)
if leng >= 7 or leng < 10:
    num = million(numberWord)
print (num)
答案

您显然在以下位置添加了千元:

if length == 6 or length == 5:
    word = pref + " Thousand " + word

千种方法。尝试将“千”更改为其他内容,您将看到:)

以上是关于将数字更改为不起作用的单词的代码的主要内容,如果未能解决你的问题,请参考以下文章

将局部变量更改为全局变量不起作用

将 nivoslider 编号更改为项目符号,即使使用主题也不起作用

将属性更改为只读不起作用

将 UIImageView 从正常更改为突出显示不起作用

如果语句没有执行。更改为开关时,仅执行默认情况

JQuery背景颜色动画不起作用