下面有一个代码,用户可以在其中添加用,
示例输入:三百三十五,六百一十五,三
输出应为: 335、6000501、3
下面是我的代码
print("Please type your number below")
value = input("")
string = ""
for unit_value in value.split(", "):
string = str(challenge1(unit_value.lower())) #function challenge1 code below (In my code it comes before )
print(string)
我的输出如下为什么第一次循环后代码未运行
335 #I can switch the order no matter what number I put 1st it shows
None #The 2nd and 3rd loop always show none no matter what the number
None
我添加了打印语句,以查看是否一切正常,并且是
print("Please type your number below")
value = input("")
string = ""
for unit_value in value.split(", "):
print(unit_value) #extra print added to see unit_value
string = str(challenge1(unit_value.lower()))
print(unit_value)
output is correct as below
three hundred thirty five
six million five hundred one
three
下面是功能Challenge1的详细信息
def challenge1(single_word, complete_number={}):
if not complete_number:
uniques = ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve",
"thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"]
teners = ["", "", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"]
big_nums = ["hundred", "thousand", "million"]
for index, word in enumerate(uniques):
complete_number[word] = (1, index)
for index, word in enumerate(teners):
complete_number[word] = (1, index * 10)
for index, word in enumerate(big_nums):
complete_number[word] = (10 ** (index * 3 or 2), 0)
current = 0
result = 0
for word in single_word.split():
if word not in complete_number:
print("Input is not a number. Please see the examples of how to enter number /n six /n negative seven"
" hundred twenty nine, /n one million one hundred one")
scale, increment = complete_number[word]
current = current * scale + increment
if scale > 100:
result += current
current = 0
return result + current
答案 0 :(得分:1)
编辑了函数,您需要清除()字典,因为它是静态声明。您声明的字典是静态的,不会在每次调用时都重新创建,因此您需要清除它或像下面这样写:
def challenge1(single_word):
complete_number={}
更正的代码:
def challenge1(single_word, complete_number={}):
if not complete_number:
uniques = ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve",
"thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"]
teners = ["", "", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"]
big_nums = ["hundred", "thousand", "million"]
negative = ["negative"]
for index, word in enumerate(uniques):
complete_number[word] = (1, index)
for index, word in enumerate(teners):
complete_number[word] = (1, index * 10)
for index, word in enumerate(big_nums):
complete_number[word] = (10 ** (index * 3 or 2), 0)
current = 0
result = 0
for word in single_word.split():
if word not in complete_number:
print("Input is not a number. Please see the examples of how to enter number /n six /n negative seven"
" hundred twenty nine, /n one million one hundred one")
scale, increment = complete_number[word]
current = current * scale + increment
if scale > 100:
result += current
current = 0
complete_number.clear()
return result + current