-1

I'm doing suppose to convert a money value into a string value. Example: 10675.78 would turn to "ten thousand six hundred seventy five and 78/100 dollars

I think I'm making this much more complicated than it needs to be. any suggestions?

def money(value):

    x = str(value)
    if len(x)>8:
        return ("Error. Value exceeds limit.")
    elif len(x)==0:
        return ("Error. Must enter a value.")
    for len(x) in range(0,7):
        for x[6:8]:
            a = x[6:8]
            print(a,'/100 dollars')

        for x[3:5]:
            b = x[4:6]
            if b = '10'
                tens = 'ten'
            elif b = '11'
                tens = 'eleven'
            elif b = '12'
                tens = 'twelve'
            elif b = '13'
                tens = 'thirteen'
            elif b = '14'
                tens = 'fourteen'
            elif b = '15'
                tens = 'fifteen'
            elif b = '16'
                tens = 'sixteen'
            elif b = '17'
                tens = 'seventeen'
            elif b = '18'
                tens = 'eighteen'
            elif b = '19'
                tens = 'nineteen'
            else:
                return false

        for x[4]:
            c = x[4]
            if b = true:
                fifth = ''  
            elif c = '9'
                fifth = 'nine'
            elif c = '8'
                fifth = 'eight'
            elif c = '7'
                fifth = 'seven'
            elif c = '6'
                fifth = 'six'
            elif c = '5'
                fifth = 'five'
            elif c = '4'
                fifth = 'four'
            elif c = '3'
                fifth = 'three'
            elif c = '2'
                fifth = 'two'
            elif c = '1'
                fifth = 'one'
            elif c = '0'
                fifth = ''

        for x[3]:
            d = x[3]
            if b = true:
                fourth = ''
            elif d = '9'
                fourth = "ninety "
            elif d = '8'
                fourth = "eighty "
            elif d = '7'
                fourth = "seventy "
            elif d = '6'
                fourth = "sixty "
            elif d = '5'
                fourth = "fifty "
            elif d = '4'
                fourth = "forty "
            elif d = '3'
                fourth = "thirty "
            elif d = '2'
                fourth = "twenty "
            elif d = '0'
                fourth = ''

        for x[2]:
            e = x[2]
            if e = '9'
                third = 'nine hundred '
            elif e = '8'
                third = 'eight hundred '
            elif e = '7'
                third = 'seven hundred '
            elif e = '6'
                third = 'six hundred '
            elif e = '5'
                third = 'five hundred '
            elif e = '4'
                third = 'four hundred '
            elif e = '3'
                third = 'three hundred '
            elif e = '2'
                third = 'two hundred '
            elif e = '1'
                third = 'one hundred '



        for x[0:2]:
            j = [0:2]
            if j = '10'
                second = 'ten thousand '
            elif j = '11'
                second = 'eleven thousand '
            elif j = '12'
                second = 'twelve thousand '
            elif j = '13'
                second = 'thirteen thousand '
            elif j = '14'
                second = 'fourteen thousand '
            elif j = '15'
                second = 'fifteen thousand '
            elif j = '16'
                second = 'sixteen thousand '
            elif j = '17'
                second = 'seventeen thousand '
            elif j = '18'
                second = 'eighteen thousand '
            elif j = '19'
                second = 'nineteen thousand'
            else:
                return false

        for x[1]:
            g = x[1]
            if j = true:
                second = ''
            elif g = '9'
                second = 'nine thousand '
            elif g = '8'
                second = 'eight thousand '
            elif g = '7'
                second = 'seven thousand '
            elif g = '6'
                second = 'six thousand '
            elif g = '5'
                second = 'five thousand '
            elif g = '4'
                second = 'four thousand '
            elif g = '3'
                second = 'three thousand '
            elif g = '2'
                second = 'two thousand '
            elif g = '1'
                second = 'one thousand'
            elif g = '0'
                second = ''

        for x[0]:
            h = x[0]
            if j = true:
                first = ''
            elif h = '9'
                first = 'ninety '
            elif h = '8'
                first = 'eighty '
            elif h = '7'
                first = 'seventy '
            elif h = '6'
                first = 'sixty '
            elif h = '5'
                first = 'fifty '
            elif h = '4'
                first = 'fourty '
            elif h = '3'
                first = 'thirty '
            elif h = '2'
                first = 'twenty '
Andy
  • 49,085
  • 60
  • 166
  • 233
HossBender
  • 1,019
  • 2
  • 10
  • 23
  • This looks like homework. Learn how to use the modulus operator % . (Also known as remainder division) – Seth Hoenig Oct 16 '13 at 19:41
  • 1
    All for-loops in Python need to have `in` with them. `for x[3:5]:` will blow up. It should be: `for var in x[3:5]:` (I just picked the name `var`). Furthermore, you use `==` for comparison tests, not `=` (which is for variable assignment). –  Oct 16 '13 at 19:42
  • Noticed this- http://stackoverflow.com/questions/8982163/how-do-i-tell-python-to-convert-integers-into-words -- seems like there is a pynum2word module out there. `import num2word` and then `print num2word.to_card(10675.78)`. Otherwise, I'd avoid these if-elif clusters and go for a lookup table. IE, tens = { 10: 'ten', 11: 'eleven' ... } and match it that way. – pp19dd Oct 16 '13 at 19:42
  • http://www.daniweb.com/software-development/python/code/216839/number-to-word-converter-python – karthikr Oct 16 '13 at 21:38

1 Answers1

1

I don't see any usage to the for loops, for each part of the number you repeat the action only once.

Moreover, for cleaner code I would use dictionaries that would save the complex if structure, such as:

hDict = {'2':'twenty', '3':'thirty',...}

first=hDict[x[0]]

and so on.

Tom Ron
  • 5,906
  • 3
  • 22
  • 38