Question

I am a beginner programmer in python and I need assistance with some code. Attached is the code I have been working on. The purpose is to take a dollar amount like 305.67 and convert the number to a text phrase like "Three Hundred Five Dollars and Sixty Seven Cents". So far I have gotten most of the code to break it into text, but I have still issues with the numbers 11-19 which is a special case. I need help figuring out how the program can decide where to apply 11-19 properly and also when to delete the "ZERO" string when it is not needed. If you run the program, you will see what I mean. The loop below in the main function would run the loop given the amount you wish depending on the function. Also, there is a function called "getDollarFormatText" which will take the numerical version of money such as 305.67 or 45.13 and give you the text format of it. The issue I run into is how do i get the program to ignore the decimal and then to convert everything to the left and right of the decimal appropriately. This is a loaded issue, and I will appreciate this thoroughly. Essentially the issue is easy to fix, but I have no idea how to tackle it. The code starts with a function that tackles two digit numbers (i have already tackled the function that separates by only single digit numbers).

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
Was it helpful?

Solution 2

Ok... Lots of problems here. Lets start with some basic syntax stuff.

First of all, you need to go back and look at how formatting works in Python. It works in spaces of four, which is the equivalent of 'tab' if you are in the right processor. When you are defining a function, everything that will be contained in that function should be AT LEAST 4 spaces out. in your function "getWordForTwoDigits()" you have this:

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

That return should be four spaces out (you did this for both of your functions, BTW).

Second, your else-if structure is screwy and unneeded. Instead of what you have now, just do this:

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

Then just add more 'elif's down to 9.

Also, you say

singlesWord = getWordForDigit(int(amount)%10)

but you never define that function.

For you actual question of how to separate the 196 and the 97, try something like this:

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

The 'rsplit' basically splits the string into two parts based on the char in the first param. It returns a list ["partone", "parttwo"], so you will need to extract the two parts of the list and convert them back to ints in your main code.

Hope that helps!

OTHER TIPS

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.

use elif instead of

else:
if  

It is quick fix. For, part to handle 11-19,before

tensAmount = int(amount) / 10

use

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

but I strongly suggest you to work with dictionary, to get you started like this.

singleDigitDict={1="one", 2="Two"}#fill up
wordSingleDigit=singleDigitDict[singleDigitAmount]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top