#πŸ”’ got stuck in infinite loop

49 messages Β· Page 1 of 1 (latest)

nova copper
#

https://paste.pythondiscord.com/OOAA
this code got me stuck in infinite loop, in explore function to be exact, it supposed to find the path to the goal and return it but it append the path despite it being visited?

        """
        if goal is None:
            goal = (self.width - 1, self.height - 1)  # Default goal: Top-right corner

        path = ""
        visited = set()  # Set to track visited positions to avoid infinite loops
        while (speedster.get_x(runner), speedster.get_y(runner)) != goal:
            position = (speedster.get_x(runner), speedster.get_y(runner))
            if position in visited:
>               raise RuntimeError("Stuck in an infinite loop! Check maze configuration or movement logic.")
E               RuntimeError: Stuck in an infinite loop! Check maze configuration or movement logic.

maze.py:146: RuntimeError
===================================================================== short test summary info =====================================================================
FAILED test_maze.py::test_explore - RuntimeError: Stuck in an infinite loop! Check maze configuration or movement logic.
=================================================================== 1 failed, 7 passed in 0.88s =================================================================== 
PS D:\Education\University\High-Level\supposed> 
granite warrenBOT
#

@nova copper

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

nova copper
#

@ me when you reply to my post

urban shoal
#

You'd need to show the full code. But this isn't necessarily an infinite loop, just a path which returns so some former position. @nova copper

urban shoal
#

Ok. So looking at move, won't a 180 degree turn return you to a formerly visited position?

nova copper
#

it is supposed if the runner encounter a block returns and ontinue from another path

#

a deadend I mean

safe dagger
#

Do you still need help?

nova copper
#

yes

safe dagger
#

Tell me the full issue in detail, how urgently do you need this completed?

subtle reef
# nova copper it is supposed if the runner encounter a block returns and ontinue from another ...

Yes, you turn... But you don't let it return - you mark any previously visited position as "loop" and error. You don't store any positions to get back to or anything, you have to physically move through an already visited path

So it's

  • go to a dead end
  • turn back
  • current position is visited, kill the program

Vs

  • current position is visited but it's not a loop, so let's see where else we can turn
nova copper
#

I'm in part 3 and need to finish other 3 parts inunder 16 hours

safe dagger
#

Can you please past all the parts that need completing?

#

What level or grade is this?

safe dagger
#

I’m willing to help mate

nova copper
#

first year and first semester? but I need ot do them in sequence

safe dagger
#

Just send it here below everything that needs to be completed

subtle reef
safe dagger
#

Do you have like a google doc for all the parts that need to be done?

#

@subtle reef if I know the solution, I am able to give him it?

subtle reef
granite warrenBOT
#

8. Do not help with ongoing exams. When helping with homework, help people learn how to do the assignment without doing it for them.

subtle reef
#

"help people learn without doing it for them"

nova copper
#

also if I try without eliminating the loop it will go infinite running

subtle reef
# nova copper kind of, tho I dont know how to explain it

Okay. So DFS is basically running in a maze like this.
It means

  • run through first path you can
  • if you can't anymore, start going back until you see another path
  • repeat (run, get back a bit, run another path...)

This means you WILL have to step on paths you already visited.

It's usually done using recursion - because you can easily "go forwards" with code, but "get back until you can see some unvisited path" is harder... But with recursion, ending the nested call will move us back πŸ˜„

nova copper
#

okay I can see that, but how to do it, it is kinda hard to imagine

subtle reef
#

In your case, your problem was running back - you didn't have any idea how to run back, so you repeated the normal movement code... Thus getting stuck.

When going forward, deeper into the maze, you want to step only on the not visited things.

But going back is another story - as I said, usually nicely done with recursion

urban shoal
#

One way to think of it is: you're saying never visit a position more than once. Butyou may really want: mark a position as not to be visited only if you've exhausted all the choices from that position, rather than marking it immediately.

nova copper
#

I see, I store it in list of tuples that contains what's been visited, but even so I cant imagine how I would tell the program that it will return to a certain point that has a way

subtle reef
#

As I said, the recursion is used for DFS because the "get back" is done by it already - exiting one level of nesting == getting back to the old position.

Pseudocode for recursive DFS:

def recursion(x, y...):
    for (possible not visited paths here):
        #mark visiting
        recursion(new x, new y) #try the path - run deeper and deeper
        
        # code being here means we exited one nesting... and we want to check next one - we're in the loop, so it's not a problem
    # here outside the loop it means "no unvisited paths left
    # so we just end the function to get back - think if you need to return anything

If you need to store the movements like you do in your code - do you need to keep all turns you took ever, or only the ones that get you to the end?

If only the ones that take you to the end, then it would be another variable passed to the function.

#

Wiki has more readable pseudocode https://en.m.wikipedia.org/wiki/Depth-first_search

Depth-first search (DFS) is an algorithm for traversing or searching tree or graph data structures. The algorithm starts at the root node (selecting some arbitrary node as the root node in the case of a graph) and explores as far as possible along each branch before backtracking. Extra memory, usually a stack, is needed to keep track of the node...

nova copper
#

I still couldnt think of anything

#

coming in 10 min

nova copper
#

okay, so I read it, and tried to do something with it, but still I couldnt

#

I'm still new to programming so I didnt study any algorthim to this moment

#

@subtle reef sorry for mentioning you

subtle reef
#

Sorry, it's almost 1am and my brain is not braining anymore

nova copper
#

oh its fine

#

I'll post another one

nova copper
#

!close

granite warrenBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.