Range(5)


#1

Hi,
if I print range(5), I will get [0, 1 , 2, 4]
but if I write

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

my range(5) also contains 5(my grid is 5x5). Why?


#2

Technically speaking you would actually get [0, 1, 2, 3, 4]. This is because range(5) returns a list of integers starting at 0, and stopping before reaching 5; Hence why your list contains the numbers 0-4.

Why does it start at 0? In programming, we always start counting at 0 (For an in-depth explanation as to why this is, I suggest reading this article). This also applies to your grid…

Your grid may be 5x5, but because we start counting at 0, it is technically a 4x4 grid. Make sense?

For more information on range(), I suggest reading up on the documentation.


#3

Thanks for your response :slight_smile:


#4

A post was split to a new topic: Battleships exercise 3


#5

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