Search code examples
pythonwhile-loopnested-loops

Python nested while loop issues


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.


Solution

  • 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