运行python 3.0
我收到一个“AttributeError:'tuple'对象没有属性替换”错误代码基于三个函数来处理更高的数字(超过100万)。不幸的是,函数似乎没有执行,因为元组导致a()。
功能代码 -
def under_ten_million(value): # under 10000000
textnumber = str(test_n)
firstdigit, secondDigit, thirdDigit, fourthdigit, fifthdigit, sixthdigit, seventhdigit = textnumber
words.append(million_list[int(firstdigit)])
if int(sixthdigit) >= 2:
words.append(hundreds_list[int(secondDigit)])
words.append(decades_list[int(thirdDigit)-1])
words.append(thousand_list[int(fourthdigit)])
words.append(hundreds_list[int(fifthdigit)])
words.append(decades_list[int(sixthdigit)-1])
words.append(number_list[int(seventhdigit)])
elif int(sixthdigit) < 2:
words.append(hundreds_list[int(secondDigit)])
words.append(decades_list[int(thirdDigit)-1])
words.append(thousand_list[int(fourthdigit)])
words.append(hundreds_list[int(fifthdigit)])
if int(sixthdigit + seventhdigit) == 10:
words.append(teen_list[1])
words.append(number_list[0])
elif int(sixthdigit + seventhdigit) > 10:
words.append(teen_list[int(seventhdigit)+1])
words.append(number_list[0])
return words
(整个代码的要点是输入数字并输出所述数字的字值。我正在使用基于if语句中的输入数字使用的实现函数来执行此操作。示例如何程序运行 - 输入= 345942,输出=三十四万五千九百四十二)
正如您在下面根据打印代码看到的那样,我仍然在“under_ten_million”下面还有2个相同的代码(只有1或2个额外的行来修改新数字)。
试图打印结果的代码部分 -
# Tests where the number lies in terms of function use, then provides correct function.
string_of_words = ()
if n <= 10:
under_ten(n)
string_of_words = words[0]
elif 10 < n < 100:
under_hundred(n)
string_of_words = words[0] + " " + words[1]
elif 100 < n < 1000:
under_thousand(n)
string_of_words = words[0] + " " + words[1] + " " + words[2]
elif 1000 < n < 10000:
under_ten_thousand(n)
string_of_words = words[0] + " " + words[1] + " " + words[2] + " " + words[3]
elif 10000 < n < 100000:
under_hundred_thousand(n)
string_of_words = words[0] + " " + words[1] + " " + words[2] + " " + words[3] + " " + words[4]
elif 100000 < n < 1000000:
under_million(n)
string_of_words = words[0] + " " + words[1] + " " + words[2] + " " + words[3] + " " + words[4] + " " + words[5]
elif 10000000 < n < 100000000:
under_ten_million(n)
string_of_words = words[0] + " " + words[1] + " " + words[2] + " " + words[3] + " " + words[3] + " " + words[5] + " " + words[6]
elif 100000000 < n < 1000000000:
under_hundred_million(n)
string_of_words = words[0] + " " + words[1] + " " + words[2] + " " + words[3] + " " + words[4] + " " + words[5] + " " + words[6] + " " + words[7]
elif 1000000000 < n < 10000000000:
under_billion(n)
string_of_words = words [0] + " " + words[1] + " " + words[2] + " " + words[3] + " " + words[4] + " " + words[5] + " " + words[6] + " " + words[7] + " " + words[8]
print(string_of_words.replace(' zero', ''))
我有类似(基本相同)的功能完美地工作,这就是为什么我很困惑为什么超过100万的数字不计算。如果您想要整个代码(近250行),我也可以上传它!
谢谢!