Reputation: 81
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
Upvotes: 7
Views: 102966
Reputation: 1
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]
Upvotes: 0
Reputation: 45
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
Upvotes: 1
Reputation: 6025
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
Upvotes: 0
Reputation: 259
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
.
Upvotes: 4