15. Play it, Sam


#1


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

It just has me guess 4 times consecutively and then at the end it tells me my turn. I am not sure what i missed or really how i am supposed to tell it to print out the turn after every turn. Please help.

Guess Row: 2
Guess Col: 2
Guess Row: 2
Guess Col: 2
Guess Row: 2
Guess Col: 2
Guess Row: 2
Guess Col: 2
You missed my battleship!
Turn 4
4
O O O O O
O O O O O
O O X O O
O O O O O
O O O O O
None

and then gives me this error

Oops, try again. Did you remember to print out turn + 1 each turn?


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!"
    print "Turn", (turn + 1)
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."
        print "Turn", (turn + 1)
    elif(board[guess_row][guess_col] == "X"):
        print "You guessed that one already."
        print "Turn", (turn + 1)
    else:
        print "You missed my battleship!"
        board[guess_row][guess_col] = "X"
    print "Turn", (turn + 1)
    print turn + 1
    print_board(board)


#2

you have to indent everything by one tab after the input statements for the row and column. that way, everything will end up being a part of your for loop instead of being what is executed after the loop.


#3

lol yep that was it. thank you so much. I was racking my brain trying to figure it out.


#4

new code that works

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!"
    print "Turn", (turn + 1)
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."
        print "Turn", (turn + 1)
    elif(board[guess_row][guess_col] == "X"):
        print "You guessed that one already."
        print "Turn", (turn + 1)
    else:
        print "You missed my battleship!"
        board[guess_row][guess_col] = "X"
print "Turn", (turn + 1)
print turn + 1
print_board(board)

#5

I didnt understand..
Heres 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
for turn in range(4) :
print "Turn" + str(turn + 1)
# Everything from here on should go in your for loop!
# Be sure to indent four spaces!

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)

Same error


#6

I just learned my mistake, I didnt type turn + 1
Can you tell me what does
print "Turn", turn + 1
do exactly, I'm not sure


#7

about the code u put up, are u being asked to input four times and then getting a output on your 4th input? if so, indent EVERY LINE below guess_row and guess_col statements (ie from "if guess_row==ship_row....." all the way till "print_board(board)") by one tab

about print "Turn", turn +1:
variable turn keeps a track of the number of times the for loop is run
in the statement 'for turn in range(4)' turn takes on the values 0, 1, 2, 3 so that the loop runs 4 times. Now u want the user (inputter) to know which turn they are on. when the loop runs the first round, turn =0, and if u print "Turn", turn+1, the calculation is (0+1) and output is Turn 1. similarly, for the next round of the loop, turn=1, calculation is (1+1), output is Turn 2

the , (comma) character in the print statement makes "Turn" and turn +1 print on the same line with a pace in between

Hope i have answered your question


#8

Yeah thanks
I didn't know about the comma operator in print so what I tried was
print "Turn" + str(turn + 1)


#9

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