17. A Real Win


#1

Hi everyone! I have difficulty breaking my code.

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

This is my error message:
Oops, try again. Your code looks a bit off. Check the Hint if you need help! Your code threw the following error: 'break' outside loop (python, line 33)

I expected a break to occur.

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!
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!"
        if turn == 3:
            print "Game Over"
        board[guess_row][guess_col] = "X"
    # Print (turn + 1) here!
    print (turn + 1)
    print_board(board)
for turn in range(4):
    print "Turn", turn + 1


#2

this comments:

# Everything from here on should go in your for loop!
# Be sure to indent four spaces!

are a pretty big clue?


#3

# Everything from here on should go in your for loop!

Pay attention to the above comment in your code. The code below it should go in a for-loop.

Also, you should get a NameError for "turn" variable because it seems to be undefined.


#4

Hi stetim94,
I indented 4 spaces for break as such, but hmm... the error message still appears.:


#5

Hi gvm, I am sorry but I do not understand what you meant when you wrote this:
Also, you should get a NameError for "turn" variable because it seems to be undefined.


#6

read the comment again. It tells you, you place the loop at the wrong place

Place the loop at the right place, then indent everything correctly so that its inside the loop


#7

am I supposed to right a "for" somewhere actually? Or is it just misplacement of the loop?


#8

you have a for loop:

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

just at the wrong place as this comments:

# Everything from here on should go in your for loop!
# Be sure to indent four spaces!

tell you

The idea of the for loop is that the user can have multiply guesses to guess where you battle ship is. Which means for each guess you have to validate if the ship is hit, and handle misses. So that should all go inside the for loop


#9

Oh I managed to solve it! Thank you for your help!
May I ask one thing?


Why does the board show the actual ship row and column right under the board when these numbers are not supposed to be there?


#10

you found a corner case, you didn't actually solve it. You only need one loop. (which you place correctly on line 28)

remove the loop from line 36

then you need to indent everything after line 30 by 4 spaces, to place it inside the loop to give the user multiply guesses and handle each user guess.

Please think about the game flow of your program. Programming is just not writing code, its also thinking: is my code logic? Does everything works as expected? Is my game flow logic?


#11

Yes, I managed to solve it! Thank you very much for your help! :smiley:


#12