Play it, Sam... print each turn


#1

I tried so hard to read other codes and solve this without posting but it keeps raising error:
Did you remember to print out turn + 1 each turn

MY CODE:

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 "Let's play Battleship!"
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

Everything from here on should go in your for loop!

Be sure to indent four spaces!

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


#2

print turn + 1

#3

Same result. This is so frustrating


#4

I got mine to work by making my for statement have a range of 5 instead of 4:

for turn in range(5):


#5

for turn in range(4)
turn = turn + 1 #Using your method
print "Turn", turn # Be sure to print "Turn" and the actual increment of your turn. OUTPUT= Turn1...4

OR
for turn in range(4)
print "Turn", turn+1


#6

ok, don't worry.
your code is this:
`

# Print (turn + 1) here!
turn = turn + 1
print 'Turn'
print_board(board)

that is printing Turn when it should be printing THE VARIABLE "turn"

so replace the bit under the # (comment) with this

# Print (turn + 1) here!
turn = turn + 1
print turn
print_board(board)


#7

Hey!
The code is:

Everything from here on should go in your for loop!

Be sure to indent four spaces!

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!"

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_board(board)

It doesn't work when I iterate through the 3rd one. Look what it says:

Traceback (most recent call last):
File "python", line 29, in
ValueError: invalid literal for int() with base 10: ''

The program says that " guess_col = int(raw_input("Guess Col:")) " doesn't work when I iterate

PLEASE REPLY FAST!!


#8

I got the same error, but only when I didn't enter a number at all for one of the guesses.


#9

Still having an issue with this. I've no idea what they want. The code works fine, it's pasted below but it keeps giving me the annoying "Did you print the turn" error. Can someone share a working version of the code? Mine is below (I've tried several variations, none of them get rid of the error message, even though all of them seem to do the job correctly)

for turn in range(4):
    print "Turn", str(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."
        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_board(board)

#10

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

just don't forget at the beginning of the program to initialize the 'turn' var (turn = 0)


#11

I already tried that and doesn't work. Did that work for you?

If anyone has a code that actually works on the codeacademy interface, please let me know. It's killing my OCD to have that section unfinished.


#12

Here is the full code and it worked for me-

from random import randint
board = []
turn = 0
for x in range(5):
board.append(["O"] * 5)

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

print "Let's play Battleship!"
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 i in range(4):
print "Turn", turn + 1
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!"
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)

#13

And be sure to check that you have 4 spaces beneath every code line with colon at the end


#14

Thanks, it worked!

I also realized what is the issue. Apparently, if you have the break statement after the first if statement, it evaluates your code as incorrect! You have to wait for Exercise 16 to add the break statement. Hope that helps somebody down the road!


#15

I raged for a long time on this, I found the problem in the print command not being recognised. print("Turn", turn + 1) functions, but will not return the correct answer. Bite the bullet and just do print "Turn", turn + 1.

I hope this helps. Working code below

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 "Let's play Battleship!"
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

Everything from here on should go in your for loop!

Be sure to indent four spaces!

for turn in range(4):
print "Turn", turn + 1 #print("Turn", turn + 1) does not work
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!"
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)

#16

this is what i got and it works fine. code has been switched in this one where it was already giving the answer in a print line so i removed them placed a turn = 0 in the beginning of the code so it adds +1 correctly when it iterates.

from random import randint
turn = 0
board = []

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

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

print "Let's play Battleship!"
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)


# Everything from here on should go in your for loop!
# Be sure to indent four spaces!
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!"
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)

#17

Hey,
I think initializing turn=0 can be put anywhere but in the [for something in range(4)] function
inside the for function you should start by printing the turn so : print "Turn, turn+1"
In the end of the for function you should write:
turn=turn+1
that way you could tell the program to start at turn 2 in the second guess instead of turn 1 again.
Hope it helps im new to this also (just figured it out)
tell me if you need me to post the code


#18

Thank you so much i was struggling a lot


#19