14/19 Why if I type 5, it says its out?


#1

Hi,

So thats basically my question. Why if I type 5, it says its out? I know Python starts counting from 0, and therefore if I type 5 it counts as 6. But then, we didn't fix than with?:

def random_row(board):
return randint(0, len(board) - 1)

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

Basically a "player" will never introduce 0...


#2

This doesnt make it so the player cant type a number other than 0-4, and if you type 5 its not on the board so it tells you that you chose a number that isnt even on the board


#3

@jaruizso @ryebread4

Those functions are returning a number 0-4 which will work for a board that starts and index 0 and ends at index 4. For the player input I have added some code for you.

To ensure that the response fits in your board you would have to do something like the following.

def get_input(valid=False):
    while not valid:
        try:
            ask = int(raw_input("Enter a number 1-5") - 1)
            if 0 =< ask < 5:
                valid = True
                return ask
        except TypeError:
            pass

Edit:

I Misread so I have corrected my post.