-1

A math challenge for class asks us to take words (like five seven six three) and change that to an integer (like 5763) to use in a calculation within the program. There are several examples of words2number that take words that spell out a number (like five hundred and sixty five, as if you're writing a check). But I want to take the individual words and place them into their corresponding number.

nine nine five zero = 9950, six three five = 635, etc.

freklz
  • 1
  • 1
  • I have the dict, but I cannot figure out the logic. when I get the words, how to i iterate the dict and get the numbers out and put them into a single int? – freklz Jan 30 '20 at 21:41
  • Does this answer your question? https://stackoverflow.com/questions/493174/is-there-a-way-to-convert-number-words-to-integers Answers given here satisfy your examples, but they don't look "as if you're writing a check" – Hymns For Disco Jan 30 '20 at 21:55
  • 2
    Does this answer your question? [Is there a way to convert number words to Integers?](https://stackoverflow.com/questions/493174/is-there-a-way-to-convert-number-words-to-integers) – zamir Jan 30 '20 at 22:16

1 Answers1

3

You can use this:

w_to_n = {'zero':'0', 'one':'1', 'two':'2', 'three':'3', 'four':'4',
     'five':'5', 'six':'6', 'seven':'7', 'eight':'8', 'nine':'9'
}

words ='nine nine five zero'
number = str()
for word in words.split():
    number += w_to_n[word]
int(number)

number:

 9950
CC7052
  • 563
  • 5
  • 16