我有一个用户给定的年间隔(例如1998-2017年),必须从列表“mlist”中打印特定值,该列表包含给定间隔中的任何年份。在“plist”中按索引打印出书名。 问题是,它无法在名单中找到具体年份,但它无论如何都在那里
已经尝试过用range(startingIn,endingIn)做for循环,但没用
interval = input("Enter the range: ") #User selects specific interval
startingIn = int(interval.split('-')[0])
endingIn = int(interval.split('-')[1])
for x in range (int(startingIn), int(endingIn)):
if x in mlist:
value = mlist.index(x) #mlist is the list which has years of the books
print (value, x) #plist is the list of books' names
else:
continue
plist=[“book1”,“book2”,“book3”] mlist=[“1935”,“1990”,“1980”]
它必须在用户指定的时间间隔内打印年份和书籍
您没有指定}:
mlist
和plist
列表的结构,但我怀疑您应该print(plist[value], x)
而不是{{cd1>这是一个问题。但是
x
是整数,1935
不是"1935"
,所以你永远不会得到与mlist.index(x)
匹配的结果。尝试将mlist
转换为整数列表。在这对我有用。它打印:
mlist.index(x)
返回元素的索引,因此您可能希望使用顺便说一句: 您可能不需要continue语句-在本例中它不起任何作用。在
相关问题 更多 >
编程相关推荐