1

I am relatively new to coding and I am doing a project where I make a game which is similiar to battleships e.g. 2 players place 10 ships on a 'board' and I have made a 2d array for each player where 0 = no ship and 1 = ship. I have made it so that the first player has to input an x and y value for each ship from 0-7 and if they do not input from 0-7 it is in a while loop so it keeps asking them to input a value from 0-7. I would also like to make it so that if a set of coordinates has already got a ship on it, then it asks the player to input a new set of coordinates. However, I'm having trouble with the nested while loop.

The problem:

When I run this, and I input the 2 same coordinates, it repeats "You already have a tank placed here. Please input another set of coordinates.". I think when it runs through the outer loop it maybe skips the inner while loops also?

p1Board = [[0, 0, 0, 0, 0, 0, 0, 0],[0, 0, 0, 0, 0, 0, 0, 0],[0, 0, 0, 0, 0, 0, 0, 0],[0, 0, 0, 0, 0, 0, 0, 0],[0, 0, 0, 0, 0, 0, 0, 0],[0, 0, 0, 0, 0, 0, 0, 0],[0, 0, 0, 0, 0, 0, 0, 0],[0, 0, 0, 0, 0, 0, 0, 0]]
flag3 = True
flag4 = True
var1 = True

#Player 1 Tank 2 
while var1 == True:
  while flag3 == True:
    p1XCoordTank2 = int(input("\nOn which x-coordinate would you like to place your second tank? "))
    flag3 = False
    if p1XCoordTank2 < 0 or p1XCoordTank2 > 7:
      print("Please choose an x-coordinate that is in the range 0-7. ")
      flag3 = True
  while flag4 == True:
    p1YCoordTank2 = int(input("On which y-coordinate would you like to place your second tank? "))
    flag4 = False
    if p1YCoordTank2 < 0 or p1YCoordTank2 > 7:
      print("Please choose an y-coordinate that is in the range 0-7. ")
      flag4 = True
  if p1Board[p1XCoordTank2][p1YCoordTank2] == 1:
    print("You already have a tank placed here. Please input another set of coordinates. ")
    var1 = True

Not sure how to fix it. Please may I get some help.

11
  • Please be more specific than "I'm having trouble with the nested while loop" in your error description. Include all debugging details. What's the expected output/behavior for a given input? What's the actual output? Is there an error message? Commented Nov 17, 2021 at 10:42
  • 1
    Sorry, it's my first time using stackoverflow. Hopefully I've edited it in more detail this time. Commented Nov 17, 2021 at 10:45
  • I would advise to make your variables more explanatory, p1Board explains what the variable holds (could be renamed to p1_board, to match snake casing). flag3, flag4 and var1 do not. It makes the code easier to read for yourself, but especially for others. Commented Nov 17, 2021 at 10:50
  • If the coordinate has already got a ship placed there, then p1Board[p1XCoordTank2][p1YCoordTank2] == 1. It should set var1 back to True and I would've thought that it would have gone through the whole section of code again, asking the user to input the coordinates of the second tank again. However, it just repeats the same string. Commented Nov 17, 2021 at 10:50
  • 1
    @Paul, I think it doesn't get the same error because I didn't include the section of code before which sets the coordinates for Tank1 as 1. I wasn't sure whether to include it since the main problem was in this section of code, I'll edit it now Commented Nov 17, 2021 at 11:05

1 Answer 1

1

I cannot reproduce your error where the message:

You already have a tank placed here. Please input another set of coordinates.

is printed when entering the same coordindates (f.e. p1XCoordTank2 = 0 , p1YCoordTank2 = 0).

However, to stop you while loop from running, you have to set var1 to False somewhere:

flag3 = True
flag4 = True
var1 = True

#Player 1 Tank 2 
while var1 == True:
  while flag3 == True:
    p1XCoordTank2 = int(input("\nOn which x-coordinate would you like to place your second tank? "))
    flag3 = False
    if p1XCoordTank2 < 0 or p1XCoordTank2 > 7:
      print("Please choose an x-coordinate that is in the range 0-7. ")
      flag3 = True
  while flag4 == True:
    p1YCoordTank2 = int(input("On which y-coordinate would you like to place your second tank? "))
    flag4 = False
    if p1YCoordTank2 < 0 or p1YCoordTank2 > 7:
      print("Please choose an y-coordinate that is in the range 0-7. ")
      flag4 = True
  if p1Board[p1XCoordTank2][p1YCoordTank2] == 1:
    print("You already have a tank placed here. Please input another set of coordinates. ")
    var1 = True
  else:
    p1Board[p1XCoordTank2][p1YCoordTank2] = 1 #place the tank
    var1 = False #stop the while loop
Sign up to request clarification or add additional context in comments.

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.