2
def find_starman(board):
    row_number = 0
    column_number = 0

for star in board:
        for j in range(len(board):
           for i in range(len(board):
               if star[j][i] == '*':
                    j += 1
                    i += 1
     return [j,i]   

I have no idea, everytime I ran the program, it says it is out of range? How can I fix this? (Also, forgot to say the question, when if finds the "*" it returns j+1 and i+1 )

3
  • Make sure star is a list of at least 5 lists of at least 5 elements each. From the error, it isn't. Commented Oct 20, 2017 at 23:47
  • Also, don't try to modify i and j manually, it probably won't behave as you expect. Commented Oct 20, 2017 at 23:49
  • What is board (I'm assuming is iterable of some kind). Also, make sure you past valid code. I see missing closing parentheses in the two inner for loops and the indentation is wrong. That code won't run, so is not very useful. Commented Oct 20, 2017 at 23:58

2 Answers 2

4

For for loops, you don't need to increment the iterator in the loop body as it will automatically update to the new value after each loop (in this case, an increment of 1 since you're assigning it to iterate over range with steps of 1).

In other words, these lines are unnecessary:

j += 1
i += 1

Furthermore, from your edited question, it seems you'd like to return the coordinates (i+1, j+1) of the found *'s. In that case:

1) If you want to return those coordinates for only the first * you can find, and immediately exit the function, you can do:

if star[j][i] == '*':
   return (j+1, i+1)

2) If you want to return the coordinates for all the *'s in your array, you can create new variables (like an empty list) before you build your loop, and for every run of the for loop, store the i+1 and j+1 of the found * as a child tuple/list to that variable (using append). In other words, something like this:

found_coordinates = []
for i in range(len(board)):
    for j in range(len(board)):
        if star[i][j] == '*':
            found_coordinates.append((j+1, i+1))

In any case, your iterators (i and j) are either immediately returned or stored in another object, and should not be modified (using += or something else) inside a for loop.

Sign up to request clarification or add additional context in comments.

Comments

0

You should remove j+=1 and i+=1 from your code and modify your return like this: return(j+1, i+1). I think thats what you want to do.

 if star[j][i] == '*': 
         return [j+1,i+1]  

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.