A Real Win Lesson 17 I need Help


#1



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


I have indented the break code correctly however it says "break outside loop"


Replace this line with your code. 
from random import randint

board = []

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

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

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)
guess_row = int(raw_input("Guess Row:"))
guess_col = int(raw_input("Guess Col:"))
print ship_row
print ship_col
# Write your code below!
if guess_row == ship_row and guess_col == ship_col:
    print "Congratulations! You sank my battleship!"
    break
else:
    print "You missed my battleship!"
    board[guess_row][guess_col] = "X"
    print_board(board)
if guess_row not in range(5) or guess_col not in range(5):
    print "Oops, that's not even in the ocean."
elif guess_row == ship_row and guess_col == ship_col:
    print "Congratulations! You sank my battleship!"
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)
if turn == 3:
    print "Game Over"


#2

Do you mean that you think break should be allowed where you have it, or that your break is in a loop? (neither is true)

I'm not sure what is stopping you from fixing this, what exactly is the problem, what information are you looking for?


#3

Well when I place the break function down underneath the congratulations message indented it says that it isn't in the loop. When I look at other peoples code I see that they also have different messages as well as different functions such as:
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!

guess_row = int(raw_input("Guess Row:"))
guess_col = int(raw_input("Guess Col:"))

I do not know how to fix whatever mistakes I have made higher up in the code


#4

Sounds like you just need to think a little harder about what break does and when you want that action to be carried out in your program and where in the code it will be executing at that moment.

It says break isn't allowed outside loops, so you will just have to ask yourself what you meant to do.


#5

Thank you. I will ponder it like that guy on the pedestal just leaning on my hand in that pose.


#6

Ok I still am unable to figure it out i know that the break is at the end of the for loop but that loop is far ahead in the code. Do I have to indent everything after that for loop appropriately?:
from random import randint

board = []

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

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

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)
guess_row = int(raw_input("Guess Row:"))
guess_col = int(raw_input("Guess Col:"))
print ship_row
print ship_col

Write your code below!

if guess_row == ship_row and guess_col == ship_col:
print "Congratulations! You sank my battleship!"
else:
print "You missed my battleship!"
board[guess_row][guess_col] = "X"
print_board(board)
if guess_row not in range(5) or guess_col not in range(5):
print "Oops, that's not even in the ocean."
elif guess_row == ship_row and guess_col == ship_col:
print "Congratulations! You sank my battleship!"
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)
if turn == 3:
print "Game Over"


#7

I feel like I have made a mistake earlier in the code still and i don't know how to correct it


#8

Aren't you contradicting yourself there? How would it be at the end of something that comes later?

The answer to whether you have to indent appropriately is yes no matter what piece of code you're talking about because indentation has meaning to the interpreter in python.

What you posted won't run at all because the formatting is all messed up, you need to take care that code is intact when you share it, otherwise you shared something else!
But I can tell that there's no break at all, so if there's supposed to be a break, then you'll need to think about when your program should do so. It should keep executing turns until the game is won at which point it should exit the loop, no?

If you were to write instructions to a human you would use a human language such as English, the ordering of those instructions would be the same as they are in code. Perhaps you should start with writing down what it should do in English, read through it and convince yourself of that the order is correct, and then translate it to code, or just use it to correct what you have until they match.

You can also use a smaller piece of code to experiment with how loops and break statements interact so that you can experiment with that in isolation of the rest of your code.


#9

Ok I have been looking within and found a problem. It says that turn is not defined. I am wondering how can it be defined?:


#10

Same as you define any other variable


#11

So I just do def turn?


#12

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