Wiicked Calculator
Wiicked Calculator

Reputation: 81

TypeError: '<' not supported between instances of 'list' and 'int'

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

Answers (4)

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

Simi Lika
Simi Lika

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

Hamza
Hamza

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

An Khang
An Khang

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

Related Questions