Play it, Sam!


#1

Hello everyone!

https://www.codecademy.com/en/courses/python-beginner-en-4XuFm/2/1?curriculum_id=4f89dab3d788890003000096

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!"
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."
    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"
    # Print (turn + 1) here!
    print_board(board)
for turn in range(4):
    print "Turn", turn + 1


#2

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:

e.g.

turn = 0
    while turn < 5:
        if...
        elif...
        else...
    turn += 1

something like that


#3

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


#4

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


#5

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_board(board)
print "Turn", turn + 1

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


#6

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