0

我是 python 的初学者程序员,我需要一些代码方面的帮助。附件是我一直在处理的代码。目的是取一个像 305.67 这样的美元金额,并将该数字转换为像“305 美元和 67 美分”这样的文本短语。到目前为止,我已经获得了将其分解为文本的大部分代码,但我仍然对数字 11-19 存在问题,这是一种特殊情况。我需要帮助弄清楚程序如何决定在何处正确应用 11-19,以及何时删除不需要的“ZERO”字符串。如果你运行这个程序,你就会明白我的意思。主函数中的下面的循环将根据您希望的函数运行循环。此外,还有一个名为“getDollarFormatText”的函数,它将获取货币的数字版本,例如 305.67 或 45。13并给你它的文本格式。我遇到的问题是如何让程序忽略小数点,然后将所有内容适当地转换为小数点的左侧和右侧。这是一个加载的问题,我将彻底理解这一点。基本上这个问题很容易解决,但我不知道如何解决它。代码以处理两位数字的函数开头(我已经处理了仅由一位数字分隔的函数)。

def getWordForTwoDigits(amount):

#This value uses integer division to get the number of tens within a number.

    tensAmount = int(amount) / 10

#This variable uses the first function above to find the equivalent single number within the number given using modulo. (Zero through 9)

    singlesWord = getWordForDigit(int(amount)%10)

#For this decision structure, the structure is set to figuring out the number of tens within a number and appropriately naming it after its correct name (ten, twenty, etc.)


    if tensAmount == 1:

        wordTen = "TEN"

    else:

        if tensAmount == 2:

            wordTen = "TWENTY"

        else:

            if tensAmount == 3:

                wordTen = "THIRTY"
            else:

                if tensAmount == 4:

                    wordTen = "FORTY"

                else:

                    if tensAmount == 5:

                        wordTen = "FIFTY"

                    else:

                        if tensAmount == 6:

                            wordTen = "SIXTY"

                        else:

                            if tensAmount == 7:

                                wordTen = "SEVENTY"

                            else:

                                if tensAmount == 8:

                                    wordTen = "EIGHTY"

                                else:

                                    if tensAmount == 9:

                                        wordTen = "NINETY"


return "%s-%s"%(wordTen, singlesWord)

########################################

def getWordForThreeDigits(dolamount):

    hundredAmount = int(dolamount) / 100

    twoDigits = getWordForTwoDigits(int(dolamount) % 100)

    if hundredAmount == 0:

        return twoDigits

    else:

        if hundredAmount == 1:

            wordHun = "ONE HUNDRED"

        else:

            if hundredAmount == 2:

                wordHun = "TWO HUNDRED"

            else:

                if hundredAmount == 3:

                    wordHun = "THREE HUNDRED"

                else:

                    if hundredAmount == 4:

                        wordHun = "FOUR HUNDRED"

                    else:

                        if hundredAmount == 5:

                            wordHun = "FIVE HUNDRED"

                        else:

                            if hundredAmount == 6:

                                wordHun = "SIX HUNDRED"

                            else:

                                if hundredAmount == 7:

                                    wordHun = "SEVEN HUNDRED"

                                else:

                                    if hundredAmount == 8:

                                        wordHun = "EIGHT HUNDRED"

                                    else:

                                        if hundredAmount == 9:

                                            wordHun = "NINE HUNDRED"


return "%s %s"%(wordHun, twoDigits)

####################################

def getDollarFormatText(dollarAndCents):

#how would you separate 190.67 (example) to 190 and 67 and and give the text form for eacn
4

3 回答 3

3

For the purpose of your practice we adapted an existing nice solution ref for converting numbers to words as follows:

def numToWords(num,join=True):
    '''words = {} convert an integer number into words'''
    units = ['','one','two','three','four','five','six','seven','eight','nine']
    teens = ['','eleven','twelve','thirteen','fourteen','fifteen','sixteen', \
             'seventeen','eighteen','nineteen']
    tens = ['','ten','twenty','thirty','forty','fifty','sixty','seventy', \
            'eighty','ninety']
    thousands = ['','thousand','million','billion','trillion','quadrillion', \
                 'quintillion','sextillion','septillion','octillion', \
                 'nonillion','decillion','undecillion','duodecillion', \
                 'tredecillion','quattuordecillion','sexdecillion', \
                 'septendecillion','octodecillion','novemdecillion', \
                 'vigintillion']
    words = []
    if num==0: words.append('zero')
    else:
        numStr = '%d'%num
        numStrLen = len(numStr)
        groups = (numStrLen+2)/3
        numStr = numStr.zfill(groups*3)
        for i in range(0,groups*3,3):
            h,t,u = int(numStr[i]),int(numStr[i+1]),int(numStr[i+2])
            g = groups-(i/3+1)
            if h>=1:
                words.append(units[h])
                words.append('hundred')
            if t>1:
                words.append(tens[t])
                if u>=1: words.append(units[u])
            elif t==1:
                if u>=1: words.append(teens[u])
                else: words.append(tens[t])
            else:
                if u>=1: words.append(units[u])
            if (g>=1) and ((h+t+u)>0): words.append(thousands[g]+',')
    if join: return ' '.join(words)
    return words

#example usages:
print numToWords(0)
print numToWords(11)
print numToWords(110)
print numToWords(1001000025)
print numToWords(123456789012)

results:

zero
eleven
one hundred ten
one billion, one million, twenty five
one hundred twenty three billion, four hundred fifty six million, seven hundred
eighty nine thousand, twelve

Note that it works for integer numbers. Nevertheless it is trivial to divide a float number into two integer parts.

于 2013-10-05T03:42:44.810 回答
2

好的...这里有很多问题。让我们从一些基本的语法开始。

首先,您需要回过头来看看格式化在 Python 中是如何工作的。它在四个空格中工​​作,如果您使用正确的处理器,则相当于“制表符”。当您定义一个函数时,该函数中包含的所有内容都应该至少有 4 个空格。在你的函数“getWordForTwoDigits()”中你有这个:

def getWordForTwoDigits(amount):
...
return ...

该返回应该是四个空格(顺便说一句,你为你的两个函数都做了这个)。

其次,您的 else-if 结构是古怪且不需要的。而不是你现在拥有的,只需这样做:

if TensAmount == 1:
    do something
elif TensAmount == 2:
    do something different

然后只需添加更多的 'elif' 到 9。

还有,你说

singlesWord = getWordForDigit(int(amount)%10)

但你永远不会定义那个函数。

对于如何分离 196 和 97 的实际问题,请尝试以下操作:

def splitter(num):
    sep=str(num).rsplit('.', 1)
    return sep

'rsplit' 基本上根据第一个参数中的字符将字符串分成两部分。它返回一个列表 ["partone", "parttwo"],因此您需要提取列表的两部分并将它们转换回主代码中的整数。

希望有帮助!

于 2013-10-05T03:08:50.063 回答
1

使用elif而不是

else:
if  

这是快速修复。For, part to handle 11-19, before

tensAmount = int(amount) / 10

利用

if 10<amount<20:
#your statements to handle 11-19
else:
#your regular statements to divide by 10 again

但我强烈建议你使用字典,让你像这样开始。

singleDigitDict={1="one", 2="Two"}#fill up
wordSingleDigit=singleDigitDict[singleDigitAmount]
于 2013-10-05T03:06:31.520 回答