15.Play, it sam


#1




I am getting error "do you remember to print out turn+1 in each turn."


Replace this line with your 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!"
    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",turn + 1) 
    print_board(board)

Help me guys..i am stuck on it for long time.Thanks


#2

Try doing

print ("Turn "+(turn+1))


#3

Or with a for loop (I love loops)

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

#4

nope,...u cannot concatenate string and int


#5

No luck..i tried it already.


#6

Oops!

print ("Turn "+str(turn+1))


#7

I had the same issue, you guys helped me! thanks...


#8

How did you get over the error..kindly suggest the solution


#9

The problem here is that the system is requiring to see if the "turns" are increasing, which means if you guess right, you get the error message.

To avoid this error, just get all answers wrong and see.

Here is my code that is working fine. ( careful with the indentation )

for turn in range (0, 4):
turn = turn + 1
guess_row = int(raw_input("Guess Row from 0 to 4:"))
guess_col = int(raw_input("Guess Col from 0 to 4:"))

if guess_row == ship_row and guess_col == ship_col:
    print " ####### Congratulations! You sunk my battleship! ########"
    board[guess_row][guess_col] = "T" #the T is the battleship
    print_board(board)
    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", turn + 1# Print (turn + 1) here!
print_board(board)

if turn >= 4:
    print "Game Over"
    print " ###### YOU MISSED ALL SHOTS !!!!! ######"
    print "The answer was: Row number " + str(ship_row) + " and Col number " + str(ship_col)

#10

try for i in range(4):
turn=i+1
then print turn where you are supposed to


#11

:cry:
I am getting the same error


#12

turn starts out as zero (outside the loop) and then increases after each guess

turn = 0
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!"
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", turn + 1 # Print (turn + 1) here!
    print_board(board)

#14

Just print turn+1

Sample Code :

for turn in range(4):
#print "Turn " + str(turn+1)
print turn+1
guess_row = int(raw_input("Guess Row:"))
guess_col = int(raw_input("Guess Col:"))


#15

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