Play it, Sam!


Hello everyone!

The code is able to run, but I think that something is incorrect. I expected the 1, 0 not to appear below the first table, as well as the Turn 1, Turn 2, Turn 3 and Turn 4 at the bottom of the code.

Here is part of my code:
if guess_row == ship_row and guess_col == ship_col:
    print "Congratulations! You sunk my battleship!"
    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."
    elif(board[guess_row][guess_col] == "X"):
        print "You guessed that one already."
        print "You missed my battleship!"
        board[guess_row][guess_col] = "X"
    # Print (turn + 1) here!
for turn in range(4):
    print "Turn", turn + 1


I'm not sure about the 1,0 but the reason your 4 "turn...." statements appeared is because the program iterated the range of 4 (took 4 trips) and on each trip you told the program to first print "Turn" and then to add one to the value, then do it again, and so on.

Have you considered a while loop instead:


turn = 0
    while turn < 5:
    turn += 1

something like that


Oops. Start with turn = 1, not turn = 0


oops again. I should not have indented the while loop. my bad.


yourif elseshould be part of yourforloop andguess_rowandguess_col variable too
becasue everytime we want to ask the user for input and want to check user is right or wrong according to the condition.

Also Be sure to indent four spaces!
and here

print "Turn", turn + 1

it's upto you where you want to show the trun after printing the board or before that


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