Created Full Game but always shows Oops, that's not even in the ocean


#1

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 = raw_input("Guess a number")
guess_col = raw_input("Guess a number")

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 + 1) here!

    print "Turn",turn + 1

    if "Turn" == 3:
        print "Game Over"

    print_board(board)

I dont know what is the problem but whenever I enter a number its shows only one msg "Oops, that's not even in the ocean." and gives me no error...

Please help me @factoradic


#2

You should double check this.

Hint: If you have a < this means less than

and > is greater.

10 > 1
1 < 10

#3

@zeziba Thanks for reply.. but I think Its say if guess_row is less than 0 (i.e. if player enter number in negative ) or guess row greater than 4 (i.e. if player enters a bigger amount i.e. greater than 4) same thing for guess_col. so it will show "opps thats not even in the ocean"... because we have "O" from 0 to 4... i.e. 5 "O" and we have to make a grid of 5x5 so I think its correct!!

0 1 2 3 4
OOOOO 0
OOOOO 1
OOOOO 2
OOOOO 3
OOOOO 4

see this is what I'm trying to say you..

Please rectify me If I'm wrong anywhere....

Thanks in advance.

--
Regards
Pradeep


#4

This says

guess_row < 0 == guess_row less than 0

OR

guess_row > 4 == guess_row greater than 4

Now do you get it?

If not lets put some stuff into the IDE

1 < 0
# OUTPUT: False
1 > 0
# OUTPUT: True

I hope that clarifies everything.


#5

@zeziba Well I got my problem And fyi this is still right:

“if (guess_row < 0 or guess_row > 4) or (guess_col < 0 or guess_col > 4):”

but the problem was here:

for turn in range(4):
guess_row = raw_input(“Guess a number”)
guess_col = raw_input(“Guess a number”)

I forgot to right ‘int’ before raw_input(“Guess a number”). So that is why I was getting only one msg…

you can check it over here

labs repl.it
and select python

Here is 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):
guess_row = int(raw_input(“Guess a number”))
guess_col = int(raw_input(“Guess a number”))

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)