Bug in the final code


#1

Even after finishing the complete code there is still one bug in the code given, which I found it in testing the code.

If the guess_row and guess_col are out of the range i.e., >5, in the LAST TURN, then it is just showing “Oops! that’s not even in the ocean”, but it is not saying “Game Over”.

So, another ‘if’ statement is required to correct this bug at this part of code, where turn number is checked.


#2

In what you coded? Or in the get solution from the exercise? what is the code? Last time i did this exercise, it worked fine


#3

Yeah, it works fine.

But try giving the guess_row and guess_col … out of range … in the last turn …

generally we expect “Game Over” message, if we are unable to find the battle ship, in the given number of turns.

But in this special case, i.e., out of range inputs in the last turn, “Game over” is not shown.

try it once.


#4

Can i see your code then? Then i can understand it better


#5
from random import randint

board = []

for x in range(5):
  board.append(["O"] * 5)

def print_board(board):
  for row in board:
    print " ".join(row)

print_board(board)

def random_row(board):
  return randint(0, len(board) - 1)

def random_col(board):
  return randint(0, len(board[0]) - 1)

ship_row = random_row(board)
ship_col = random_col(board)
print ship_row
print ship_col


for turn in range(4):
  print "Turn", turn+1
  guess_row = int(raw_input("Guess Row: "))
  guess_col = int(raw_input("Guess Col: "))

  if guess_row == ship_row and guess_col == ship_col:
    print "Congratulations! You sunk my battleship!"
    break
  else:
    if (guess_row < 0 or guess_row > 4) or (guess_col < 0 or guess_col > 4):
      print "Oops, that's not even in the ocean."
      _**if turn==3:**_

 _**print "Game Over"**_
    elif(board[guess_row][guess_col] == "X"):
      print "You guessed that one already."
    else:
      print "You missed my battleship!"
      board[guess_row][guess_col] = "X"
      if turn==3:
       print "Game Over"
        print_board(board)

ACTUALLY, THE PART IN BOLD AND ITALIC WAS NOT INCLUDED IN THE CODE…BUT IF WE ADD IT … CODE BECOMES BETTER


#6

uhh…!!!sorry about the indentation… I should’ve done something else better


#7

you didn’t follow the instructions correctly:

Your new if should go under the else that handles all of the incorrect guesses, but outside of any of the nested conditions. This is because it doesn’t matter why the guess is wrong; after 4 wrong guesses, the game is over.

else: # else that handles incorrect guesses
    # nested if condition
    if (guess_row < 0 or guess_row > 4) or (guess_col < 0 or guess_col > 4):
      print "Oops, that's not even in the ocean."
      _**if turn==3:**_

 _**print "Game Over"**_
    # nested elif condition
    elif(board[guess_row][guess_col] == "X"):
      print "You guessed that one already."
    # nested else condition
    else:
      print "You missed my battleship!"
      board[guess_row][guess_col] = "X"
      if turn==3:
       print "Game Over"
        print_board(board)

see comments i added, you add the if turn == 3 into the nested else condition, not the else clause that handles incorrect guesses, which is why your code is giving unexpected behaviour.


#8

Ok @stetim94 I’ll do it once again. Thanks for helping me :slight_smile:


#9

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.