A Real Win


#1

Hi guys,

Whats wrong with my code???

import random

board = []

for x in range(0,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 random.randint(0,len(board)-1)

def random_col(board):
  return random.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!
maxturns = 4
for turn in range(maxturns):
 print_board(board)
 print turn + 1
 guess_row = input("Guess Row:")
 guess_col = 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"
if turn == 3:
print "Game Over"
# Print (turn + 1) here!
    print "You are on turn: " + str(turn + 1)`

#2

No clue. Perhaps you can explain why you deem it to be incorrect in terms of what you want it to do and what it is doing instead.
Perhaps there's some information that you're missing that would allow you to fix it yourself, if so, ask for that instead!

Your indentation is way off though, is that the issue? If yes, then you'll have to explain why you think it should be the way it is, or ask for help with figuring out indentation. If not, then your post should be updated with the real code!


#3

It says syntax error on line 41. It might be easier if you give me the correct code so that I can work out whats wrong.


#4

Well, have you had a look at that line? As mentioned, there are some indentation issues.


#5

it was actually line 37 once i had taken out some spacing, and I have played around with the indentation, but got no where, so I deleted it all.


#6

Mmh don't guess though! Think about what the code is meant to do, and what that's supposed to look like.

yes:

if ... :
    ... 
elif ... :
    ...
else:
    ...

no:

if ... :
... 
elif ... :
...
else:
...

When you have for example an if-statement, you've got to specify how many of the following statements are included under that condition. In Python that is done with indentation, all statements part of a block of code is indented one level more.


#7
import random
board = []

for x in range(0,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 random.randint(0,len(board)-1)

def random_col(board):
  return random.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!
maxturns = 4
for turn in range(maxturns):
 print_board(board)
 print turn + 1
 guess_row = input("Guess Row:")
 guess_col = 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"
if turn == 3:
    print "Game Over"
    print "You are on turn: " + str(turn + 1)

#8

still got a syntax error with line 37


#9

Which is.. an else? When is that allowed? Are you meeting that restriction?


#10

I think so. It was working before I added the breaks.


#11

An else is allowed immediately after an if

There is something other than an if immediately above it, thus it's not allowed there.


#12

What should I do then? Thanks


#13

If it's supposed to be executed when some if-statement's condition is false, then move it to after that if-statement.

If that's not the case then modify it to reflect what it's meant to do.


#14

Could you please tell me what is the correct code as I'm really stuck. :frowning: Thanks
Jed


#15

Mmh, no, that's of no interest to me.
I think that you should consider what it is you are trying to do, and look up how to do that specific thing. Repeat until everything's done.

If you don't know where the problem is, then I suggest playing around with a simpler example in a separate code file, you can add things in one at a time and when it stops working as expected you know that it's the last thing you added that's incorrect.
You might also just need to review previous exercises to make sure you've got all the required building blocks. Can't continue forwards before having understood what those more advanced things build on.


#17

guess u've found the solution already.
If you haven't, create a 'for' loop just above the 'if' statement.
Reason is : 'break' functions in a for loop and not in an 'if' condition

Cheerio!


#18

can you show me the responce you enter to finish the propmt? :sunny:


#19

The instructions informs that we can use the command break to get out of a for loop.

In other words it's telling us to create a for (for x in y:) statement before the if.

So you could type something like this:

for win in 'Battleship!':
if guess_row == ship_row and guess_col == ship_col:
print "Congratulations! You sunk my battleship!"
break


#20

Don't forget to tab the relevant statements