0

'' Hey, I have tried a lot to get this code, since I haven't found any solution in python, then I have tried and solved that problem. Hope this code helps you.

The question is: Given a string of words, which represents the numbers in words or textual format. You need to write a program to print the number that is reresented in words.

IP: negative sixty nine OP: -69 ''

ones=["zero","one", "two", "three", "four", "five", "six", "seven", "eight", "nine"]
    tens=["","", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"]
    teens = ["",'','','','','','','','','',"ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"]
    hun=["hundred", "thousand", "million"]
    x=input()
    a=x.split()
    val=0
    if a[0]=='negative':
        i=1
    else:
        i=0
    while(i<len(a)):
        if a[i] in ones:
            val=val+ones.index(a[i])
        elif a[i] in tens:
            val=val+((tens.index(a[i]))*10)
        elif a[i] in teens:
            val=val+(teens.index(a[i]))
        elif a[i] in hun:
            if a[i]=='hundred':
                temp=100
            elif a[i]=='thousand':
                temp=1000
            else:
                temp=1000000
            val=val*temp
        i+=1
    if a[0]=='negative':
        print(-1*val)
    else:
        print(val)
  • 4
    In fact you didn't ask a question. Why do you think your solution is insufficient? – Alexey S. Larionov Sep 14 '22 at 10:31
  • 1
    I would use https://github.com/ShailChoksi/text2digits which is written and maintained by people on StackOverflow, see more here: https://stackoverflow.com/questions/493174/is-there-a-way-to-convert-number-words-to-integers – Cow Sep 14 '22 at 10:53

1 Answers1

0

I think this can help you a lot:

https://pypi.org/project/word2number/