Ask coding questions

← Back to all posts
Error(While loop?)
spiritsarehere (0)

Hi,
I'm trying to create battleship using python and I'm trying to make sure that the ships don't overlap but for some reason when I try and do so noting happens; I think my while loop is wrong. Could someone help me identify the problem. I've attached my code to the post.

Thank you very much

Answered by MocaCDeveloper (554) [earned 5 cycles]
View Answer
Comments
hotnewtop
MocaCDeveloper (554)

In the while loop, line 31.
You change the values of x and y, but you do not change the value assigned to check 1 and 2. So. Simple add:

check_1 = warmap[x+1][y]
check_2 = warmap[x][y+1]

So, your while loop should look like:

while check_1!="0" or check_2!="0":
    x=random.randrange(0,6)
    y=random.randrange(0,6)
    
    # Reassign
    check_1 = warmap[x+1][y]
    check_2 = warmap[x][y+1]
spiritsarehere (0)

Thanks for your reply(I totally forgot that)! However, when I run it, the grid is still not showing; is there any reason why? @MocaCDeveloper

MocaCDeveloper (554)

@spiritsarehere

it works for me :D
Make sure you have a while loop so that the game doesn't end abruptly.

spiritsarehere (0)

Hi, sorry for bugging you again, is it possible if you could send me your version of the code. Thanks a lot for your help!
@MocaCDeveloper

MocaCDeveloper (554)

@spiritsarehere

What do you mean, "your version of the code"?

spiritsarehere (0)

Well you said it worked for you and I presumed you copied it and edited it to make it work and it still isn't showing up for me
@MocaCDeveloper

MocaCDeveloper (554)

@spiritsarehere

Oh no I meant the table was printing.

spiritsarehere (0)

ah ok, is there a reason why the table does not print afterwards with the battleships ("4s" and "5"s) in place
@MocaCDeveloper