Python 正在获取Word 2 Num的输入
我试着把代码写在需要用户输入的地方,而不是把它放在打印行中。有人能帮我吗Python 正在获取Word 2 Num的输入,python,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", "s
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.
print toNum(raw_input("Enter a number: "))