获取Word 2 Num的输入

时间:2015-10-28 16:43:21

标签: python

我试图将代码写入它要求用户输入的位置,而不是将其放在打印行中。任何人都可以帮我这个吗?

def toNum(words, num={}):
    if not num:
        teens = ["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"]
        hund = ["hundred", "thousand"]


        num["and"] = (1, 0)
        for idx, word in enumerate(teens): num[word] = (1, idx)
        for idx, word in enumerate(tens): num[word] = (1, idx * 10)
        for idx, word in enumerate(hund): num[word] = (10 ** (idx * 3 or 2), 0)

        input = result = 0
        for word in words.split():
                if word not in num:
                        raise Exception(word)

                hund, increment = num[word]
                input = input * hund + increment
                if hund > 100:
                        result += input
                        input = 0

        return result + input

print toNum("seventy three hundred")
#here I want it to ask for user input instead of my having to write it in the code. Not sure how to apply it to the rest of the code. 

1 个答案:

答案 0 :(得分:0)

print toNum(raw_input("Enter a number: "))