My project does not consider row = 5 or col = 5 "the ocean"


#1



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


Well, this is kind of weird. My project allows me to continue on to the next level but If I type in row = 5 or col = 5 or both, it claims it is not part of the ocean.


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

if guess_row not in range(0,5) or guess_col not in range(0,5):
    print "Oops, that's not even in the ocean."

elif board[guess_row][guess_col] == "X":
    print "You guessed that one already."

else:
    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)


O O O O O
O O O O O
O O O O O
O O O O O
O O O O O
Guess Row: 2
Guess Col: 5
0
2
Oops, that's not even in the ocean.
None

#2

5 is not in the ocean, range does not includes its stop value

lists are zero indexed based, so the indexes are: 0, 1, 2, 3 and 4, so 5 is not in list (ocean)


#3

Thanks, apparently I assumed the players role too deeply. :smile:


#4

no, you didn't. Humans usually count from 1 to 5 and not 0 to 4 (list). You could compensate for this difference by subtracting one from user input. This way, even you range (which checks if ship is on board) works correctly :slight_smile:


#5

After I finished this project I found interesting making this game "player friendly".

I ended up doing this.

 from random import randint

board = []

for x in range(1,6):
    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(1, len(board))

def random_col(board):
    return randint(1, len(board[0]))

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

It works for the most part.

The randomisers now generate numbers from 1 to 5 but it breaks down on the line that sets "X" on the board when number 5 is chosen.

Let's play Battleship!
O O O O O
O O O O O
O O O O O
O O O O O
O O O O O
5
1
Guess Row: 1
Guess Col: 5
Traceback (most recent call last):
  File "python", line 38, in <module>
IndexError: list index out of range

#6

i thought i cover this? Looking at the values of the humans:

# human count
1 2 3 4 5
# computer count
0 1 2 3 4

given we are working with computer, which do you think is easier to manipulate: user input or the list? Right answer: user input

so after we get the user input, we want to manipulate it to compensate for counting difference


#7

Thanks, I did not get what you meant when talking about substracting from users input on your previous post.

Now works perfectly fine.

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 Row:"))-1
    guess_col = int(raw_input("Guess Col:"))-1

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

Let's play Battleship!
O O O O O
O O O O O
O O O O O
O O O O O
O O O O O
2
1
Guess Row: 0
Guess Col: 4
Oops, that's not even in the ocean.
Turn 1
O O O O O
O O O O O
O O O O O
O O O O O
O O O O O
Guess Row: 1
Guess Col: 5
You missed my battleship!
Turn 2
O O O O X
O O O O O
O O O O O
O O O O O
O O O O O
Guess Row:

Now I realise how obvious it is:


#8

that happens quit often after you solve a problem :wink:


#9

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