Play It Sam, Turn Plus One


#1

Honestly, I give up for the night. I am an experienced programmer here to just learn the gist of what Python has to offer, and the fact that I can't go to the next lesson because of a tiny typing formality, is pretty frustrating. Is there any kind soul that would help me with this?

I don't know what kind of magic checking CodeAcademy is doing behind the scenes, but my code iterates four times like asked, and prints out the correct turn integer each time. It would be very nice to provide a way to bypass this sort of thing. Granted, I have modified the code a little bit, but, anyone have a quick hack to get through this one, I am not going back and rewriting it exactly how they ask.

Whatevz, rant over.

from random import randint

board = []

# create the 5 x 5 board
for x in range(0, 5):
    board.append(["O"] * 5)
    
board_index = len(board) - 1

# displays an easier to read board
def print_board(board):
    for row in board:
        print " ".join(row)
print_board(board)

# helpers for selecting random coordinates
def random_row(board):
    return randint(0, board_index)
def random_col(board):
    return randint(0, board_index)

# Set the coordinates of the battleship
ship_row = random_row(board)
ship_col = random_col(board)

# Used to print turn number to user
turn = 0

for i in range(board_index):
    turn += 1
    print 'Turn %d' % turn
    
    # Takes coordinate guess input from the user
    guess_row = int(raw_input("Guess Row:"))
    guess_col = int(raw_input("Guess Col:"))
    
    # Handle win or wrong guesses
    if guess_row == ship_row and guess_col == ship_col:
        print 'Congratulations! You sank my battleship!'
        break
    elif guess_row <= board_index and guess_col <= board_index:
        if 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_board(board)
    else:
        if guess_row > board_index or guess_col > board_index:
            print "Oops, that's not even in the ocean."

#2

Hi bro,

I had trouble with this too. You need to enter it exactly as prompted by the guidance info (i.e. print "Turn", turn + 1). Do not use parentheses even though they are required in Python 3.0 and enter it at the end of your block of code as the second last statement (before the last statement printboard(board))._


#3

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