17.A Real Win Help me out of this...!?


#1



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

File "python", line 35
else:
^
SyntaxError: invalid syntax


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",turn+1
    if turn==3:
        print "Game Over"
    print_board(board)


#2

if guess_row == ship_row and guess_col == ship_col:
    print "Congratulations! You sunk my battleship!"
break
else:

this code, the break is wrongly indented. Which means else is not after if, as it should be


#3

i didn't get you what you said!


#4

a else should always be after a if or elif:

if condition:
   print "do something"
else:
  print "do something else"

due to your break, the else is not after if, re-indent break


#5

but where should i kept "break" statement..!?


#6

inside your if statement


#7

but i already kept inside only na..!


#8

indention determines what is inside a if statement:

if condition:
   print "i am inside a if condition"
print "i am outside a if condition"

this should be known at this point in the course


#9

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


#10

it seems i am unable to explain, i will ask someone else to explain to you


#11

okay,help me as much as faster


#12

why can't you simple understand you need to indent your break so it is inside your if statement:

if guess_row == ship_row and guess_col == ship_col:
    print "Congratulations! You sunk my battleship!"
    break
else:

#13

lol there is no indent error

my error message is shown below


#14

now all of a sudden you have a different error message....

anyway, look at the comment:

# 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:"))

not everything is inside your for loop, only what i posted above. Everything from that comment to the end of file should be in your for loop,you really need to indent better


#15

hello #stetim94,what ever i did in my code there is no indent error upto my knowledge ..!
if you want to check it once then see link of my code and errors.

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


#16

indention errors rarely show up, usually if your indention is off it causes other problems in your programs. But your indent is off. line 31 till end of file should also be inside the for loop, which is achieved by indent!!


#17

now what should i do..?


#18

make the indention of your code correct, so that everything after the for loop you created on line 28 is actually inside the for loop


#19

thankq #stetim94 you helped me alot