【发布时间】:2020-07-31 16:14:33
【问题描述】:
这段代码导致了
TypeError: 'list' object cannot be interpreted as an integer.
在添加用户输入之前它正在工作。该代码返回数字与输入数字对的范围重叠的次数。提前致谢!
user_input_1 = input("Enter the first pair here: ")
lst1 = [int(i) for i in user_input_1.split(" ") if i.isdigit()]
user_input_2 = input("Enter the second pair here: ")
lst2 = [int(i) for i in user_input_2.split(" ") if i.isdigit()]
user_input_3 = input("Enter the third pair here: ")
lst3 = [int(i) for i in user_input_3.split(" ") if i.isdigit()]
user_input_4 = input("Choose a center number: ")
number = int(user_input_4)
mainlist = [[lst2], [lst2], [lst3]]
def count_overlapping(mainlist, number):
count = 0
for element in mainlist:
if number in range(element[0], element[-1]) or number == element[0] or number == element[-1]:
count += 1
return count
print(count_overlapping(mainlist, number))
【问题讨论】:
-
你应该打印
mainlist看看它是否真的是你想要的。看起来您多了一层嵌套。 -
正如 Mark Meyer 所说,您的主列表有一个额外的级别,将其更改为
mainlist = [lst2, lst2, lst3]应该可以工作。 -
啊!谢谢,就是这样
标签: python list integer typeerror