# Setting the wrong range - Instructions

#1

<Below this line, add a link to the EXACT exercise that you are stuck at.>
<In what way does your code behave incorrectly? Include ALL error messages.>
Traceback (most recent call last):
File “python”, line 30, in
IndexError: list index out of range
<What do you expect to happen instead?>
The instructions tells us to use the “if guess_row not in range(5)…” but this will produce the above error if we introduce the coordinates 5,x or x,5, or any other value greater than 5.
2 things: either the range should be different or values equal or greater than 5 should not be allowed!

```python

rom random import randint

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)
print ship_row
print ship_col
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 sank my battleship!!”
elif board [guess_row][guess_col] == “X”:
print “You guessed that one already.”
else:
if guess_row not in range(5) or guess_col not in range(5):
print “Oops, that’s not even in the ocean.”
else:
print “You missed my battleship!”
board [guess_row][guess_col] = “X”
print_board(board)

``<do not remove the three backticks above>``

#2

Hi this part…

``````if guess_row == ship_row and guess_col == ship_col:
print "Congratulations! You sank my battleship!!"
elif board [guess_row][guess_col] == "X":
print "You guessed that one already."
else:
if guess_row not in range(5) or guess_col not in range(5):
print "Oops, that's not even in the ocean."
else:
print "You missed my battleship!"
board [guess_row][guess_col] = "X"
print_board(board)
``````

try to put the elif statment inside your else statement like that

``````
else:
if guess_row not in range(5) or guess_col not in range(5):
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_board(board)
``````

#3

Ok, I’ll try but why inside the nested if??’

#4

It’s nested inside the else statement

#5

I understand that, but why does it needs to be in the already nested if??? Why couldn’t it be like I have in my original code?

#6

Also, that was not the problem I was reporting… The problem is when we insert, for instance 4 and 4, and as the counting in python is made from 0, these coordinates would be in the lower right corner which is not what we pretend as users…

The coordinates 5,5 would be the ones to place the X in the lower right corner… But 5,5 returns a lists out of range, of course, because our rage is from 0 up to 4 and not from 1 up to 5, unless we change the code accordingly! But in that case, the site will consider the code as wrong!

#7

Because if you guess a number out of the range it will not run the else statement but the else if statement which chek if you already guess the number you input and it throw you that error

``````Traceback (most recent call last):
File "python", line 32, in <module>
IndexError: list index out of range
``````

SO when you put it inside the else statement it check before if it out of the range then if you already guess it.

#8

Ok, what can we do about the other problem I stated?

#9

About that one I don’t and I’m not so sure but the the number 1 is qual to 0 and 5 is equal 4 because the

``````range(5)

#Output

[0, 1, 2, 3, 4]
``````

it not include the number 5 its maybe for that why it will print out of the Ocean if you put 5

#10

Sure, but for the user/player of the game, the range should be something like 1 to 5 and not 0 to 4… I guess, no?

#11

#12

Ask them for input in range 1 to 5, then convert with code as so:

``````guess_row -= 1
guess_col -= 1
``````

#13

That will make the site to complain about changing the variables guess_row and guess_col, but no problem, just wanted to make sure I was right!

Another problem in the same code is when they ask to print <turn + 1> they don’t say to initialize this variable with the value zero and also that it must be initialised outside of the for loop and also they say to place this piece of code at the end of the loop but this way, the first time we run the code we only see “Turn: 1” when we’re about to start turn 2 which also is non sense!

I have the code like this, hope it’s not another missing space or another extra space not needed that is preventing the site to check the code as valid

``````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!
turn = 0
for attemps in range(4):
turn += 1
print "Turn: {}".format(turn)
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!"
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_board(board)
``````

#15

I need help to be able to proceed! I can’t make the site to accept the code as I have it in my previous post!

#16

hey @psysc0rpi0n, I placed your code into last excercise of Battleships and it worked. maybe you need to do the same (replace the code)?

#17

It is not working to me!
It is saying that:

Oops, try again. Did you remember to print out turn + 1 each turn?

#18

Hi you for loop

``````turn = 0
for attemps in range(4):
turn += 1
print "Turn: {}".format(turn)
guess_row = int(raw_input("Guess Row:"))
guess_col = int(raw_input("Guess Col:"))

``````

should be like that

``````for turn in range(4):
``````

the they aske you to print…

``````print "Turn", turn + 1
``````

you do it just like that

``````for turn in range(4):
print "Turn", turn + 1
guess_row = int(raw_input("Guess Row:"))
guess_col = int(raw_input("Guess Col:"))
``````

#19

Ok, done! This is kinda silly… Thanks