我正试图在Python中运行一段简单的代码,尝试将一个文本文件放入列表中,并得到以下错误消息:
类型错误:'<'在"list"one_answers"int"的实例之间不支持
这是代码:
def MAINLOOP ():
import random
listofkeywords = []
attempts = 0
complete = ([0,0,0,0,0,0,0,0,0,0,0,0,0,0,0])
while complete < 30:
question = random.randint(0,14)
print(question)
MAINLOOP()
def IMPORTKEYWORDS():
thekeywords = open("keywords.txt","r")
listofkeywords == thekeywords
第while complete < 30
行出现错误。complete
是一个列表,您试图将其与整数30
进行比较?如果要比较列表长度,请使用while len(complete) < 30
。
解决错误的一种方法是访问列表中的特定项。
def MAINLOOP ():
import random
listofkeywords = []
attempts = 0
complete = ([0,0,0,0,0,0,0,0,0,0,0,0,0,0,0])
while complete[0] < 30:
or
while len(complete[0]) < 30:
question = random.randint(0,14)
print(question)
MAINLOOP()
def IMPORTKEYWORDS():
thekeywords = open("keywords.txt","r")
listofkeywords == thekeywords
您正在同时比较整个列表。除非使用numpy,否则不支持这样的比较。修复您的代码是比较列表中的每个条目,如下所示:
def MAINLOOP ():
import random
listofkeywords = []
attempts = 0
complete = ([0,0,0,0,0,0,0,0,0,0,0,0,0,0,0])
for i in complete:
if i <30:
question = random.randint(0,14)
print(question)
MAINLOOP()
def IMPORTKEYWORDS():
thekeywords = open("keywords.txt","r")
listofkeywords == thekeywords
因为complete是一个元组,里面有一个列表,所以首先应该得到第一个元素,即列表
你可以使用下面的这样的索引
完成[0]