Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python less than error compare int and array length

Sorry if I accidentally duplicated a question, I am still a newbie to Python.

I am working on a school project which requires us to solve a classic riddle using graph searches. I am writing in Python because it is a good excuse for me to start learning it, but I run into issues that are strange to me.

For one section, I want to cycle through a list of explored nodes, and see if another node is the same as any already in the list of explored nodes. If it is not already explored, then it can potentially be the next node to explore in the graph.

The problem I am finding is in a line where I make a for loop to search every value in the explored list. Here is what I wrote:

def validate(self, testnode, explored):
    if((testnode.wolf == testnode.sheep != testnode.farmer) or (testnode.sheep == testnode.cabbage != testnode.farmer)):
        #return failure
        return false
    for i < len(explored):
        if testnode == explored[i]:
            #return failure
            return false
    else: return true

and here is my error

  File "AI_Lab1_BFS.py", line 54
    for i < len(explored):
          ^
SyntaxError: invalid syntax

I have read some other issues with Python users on SO where the problem was comparing the wrong types, like comparing an int to a float. I don't think this is my problem though, since len(explored) should be an int, right? That is what I have seen, though maybe I misunderstood/assumed things. If you can offer any help then I will be most grateful!

Thanks to everyone for your quick responses. The recommended changes definitely worked.

like image 849
user2828965 Avatar asked Jul 07 '26 10:07

user2828965


1 Answers

Replace for i < len(explored): with for i in range(0, len(explored)):

like image 112
musical_coder Avatar answered Jul 09 '26 07:07

musical_coder



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!