Battleship 12./19


#1



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


The code prints else: at line 6 as invalid syntax.

The code to run as intended, as the code is(I think) properly indented and with syntax.


if guess_row == ship_row and guess_col==ship_col:
    print("Congratulations! You sank my battleship!")
else:
    if guess_row not in range(5) or guess_col not in range(5):
        print("Oops, that's not even in the ocean.")
else:        
    print("You missed my battleship!")
    board[guess_row][guess_col]='X'
    print_board(board)


#2

you can't have else after else. Since else is all remaining cases, what is the second else then?

you first write if and else to determine if its a hit or miss:

if guess_row == ship_row and guess_col==ship_col:
    print("Congratulations! You sank my battleship!")
else:
    # different scenarios for missing

the else should handle the different scenarios of a miss, so:

else:        
    print("You missed my battleship!")
    board[guess_row][guess_col]='X'
    print_board(board)

should be indented with 4 spaces to be nested inside else


#3

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