Write parts 1 (convert string to lowercase) and 2 (tokenize string) of that code and incorporate the function below. Use the string.split function. Get the code to work as a cohesive unit to convert strings like "two hundred sixty two" to numbers like 262.

This is basically what I need to do.

def turn_words_into_numbers(number_list):
    output = 0
    current = 0
    for index in range(len(number_list)):
        current = eval(number_list[index])
        if(output==0):
            output=output+current 
        elif(output < current):
            output=output*current
        elif(output>current):
            output=output+current
        else:
            return('error')
        print('index:',index,'output:',output,'current:',current)
    return(output)

The code is not really correct, so any help would be greatly appreciated, I have NO idea what to do.

Nice link -ordi-, nice complement for my code which produced words from numbers.

The last version of code seems little scrambled, so I repost it here fixed:

import re

numwords = {}

def text2int(textnum):
    if not numwords:

        units = [ "zero", "one", "two", "three", "four", "five", "six",
                "seven", "eight", "nine", "ten", "eleven", "twelve",
                "thirteen", "fourteen", "fifteen", "sixteen", "seventeen",
                "eighteen", "nineteen"]

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

        scales = ["hundred", "thousand", "million", "billion", "trillion", 
                'quadrillion', 'quintillion', 'sexillion', 'septillion', 
                'octillion', 'nonillion', 'decillion' ]

        numwords["and"] = (1, 0)
        for idx, word in enumerate(units): numwords[word] = (1, idx)
        for idx, word in enumerate(tens): numwords[word] = (1, idx * 10)
        for idx, word in enumerate(scales): numwords[word] = (10 ** (idx * 3 or 2), 0)

    ordinal_words = {'first':1, 'second':2, 'third':3, 'fifth':5, 
            'eighth':8, 'ninth':9, 'twelfth':12}
    ordinal_endings = [('ieth', 'y'), ('th', '')]
    current = result = 0
    tokens = re.split(r"[\s-]+", textnum)
    for word in tokens:
        if word in ordinal_words:
            scale, increment = (1, ordinal_words[word])
        else:
            for ending, replacement in ordinal_endings:
                if word.endswith(ending):
                    word = "%s%s" % (word[:-len(ending)], replacement)

            if word not in numwords:
                raise Exception("Illegal word: " + word)

            scale, increment = numwords[word]

        if scale > 1:
            current = max(1, current)

        current = current * scale + increment
        if scale > 100:
            result += current
            current = 0

    return result + current

Edited 6 Years Ago by pyTony: n/a

Print what you have first because it doesn't look like it is doing what you would want if "number_list" is a string and has not been split into words.

def turn_words_into_numbers(number_list):
    output = 0
    current = 0
    for index in range(len(number_list)):
        current = eval(number_list[index])
        print index, number_list[index]

Some theory: it may work better to reverse the list of words first. That way, "two hundred" becomes "hundred", "two" and you can set a multiplier to 100 to multiply the next word by. I am also using tuples to store the numbers, but you can use a list of lists as well. This assumes that you are not familiar with dictionaries, which would be a better choice. A simple example (doesn't have teens or twenty, thirty, etc.):

start = "two hundred and three"
rev = start.split()
rev.reverse()
print rev

ones = ("zero", "one", "two", "three", "four")
multiplier = (("hundred", 100), ("thousand", 1000))
multiply_by = 1
total = 0
for num in rev:
    print num
    ##  search for next number in ones tuple
    if num in ones:
        idx = ones.index(num)
        total += idx * multiply_by  ## index is the same as the number
        multiply_by = 1
    else:
        for tup in multiplier:
            if tup[0] == num:
                multiply_by  = tup[1]
print total

Edited 6 Years Ago by woooee: n/a

def turn_words_into_numbers(number_list):
    output = 0
    current = 0
    for index in range(len(number_list)):
        current = eval(number_list[index])
        if(output==0):
            output=output+current 
        elif(output < current):
            output=output*current
        elif(output>current):
            output=output+current
        else:
            return('error')
        print('index:',index,'output:',output,'current:',current)
    return(output)

def convert_string_to_lower_case(number_list):
	new_string = ''
	for i in number_list:
		if 65 <= ord(i) <= 90:
			new_string=new_string+convert_character_to_lower_case(i)
		else:
			new_string=new_string+i
	return(new_string)
    
def tokenize_string(new_string):
    print(new_string.split())

def main():
    number_list = input('Enter a written number: ')
    convert_string_to_lower_case(number_list)
    tokenize_string(number_list)
    turn_words_into_numbers(number_list)
main()

My new code, though there are errors. Thanks for the help so far guys! It doesn't work still, but I hope I'm on the right track...any more suggestions?

Edited 6 Years Ago by yellowkaiq: didn't ask for input/help

This article has been dead for over six months. Start a new discussion instead.