I am trying to run a simple piece of code in Python to try and put a text file into a list and get this error message:
TypeError: '<' not supported between instances of 'list' and 'int'
This is the code:
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
Error at line while complete < 30
. The complete
is a list and you try to compare it with a integer number 30
? If you want to compare the list length, use while len(complete) < 30
.
You are comparing whole list at once. Such comparisons are not supported unless you use numpy. Fix for your code is to compare each entry in list individually as follows:
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
One way to solve the error is to access a specific item in the list.
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
because complete is a tuple and there is a list inside it then first you should get first element which is the list
you can use index like below
complete[0]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.