(0, len(board) - 1) 12/19 and 13/19


#1

tl;dr What is the "-1" and the "[0]" for in randint(0, len(board[0]) - 1) and range(0, len(board) - 1), I'm so confused on this?

So here's the code I use in 13/19:


from 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)

guess_row = int(raw_input("Guess Row:"))
guess_col = int(raw_input("Guess Col:"))

# Write your code below!
if guess_row == ship_row and guess_col == ship_col:
    print "Congratulations! You sank my battleship!"

else:
    if guess_row not in range(0, len(board)-1) or \
    guess_col not in range(0, len(board)-1):
        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)

The hint in 12/19 says:

The valid values for guess_row are 0 to rows-1 and the valid values for guess col are 0 to cols-1. You will need to build up a complex Boolean expression of the form:

The instructions say to use: range(5)

Now in 13/19 I use the:

guess_row not in range(0, len(board)-1) or \
    guess_col not in range(0, len(board)-1)

and it messes the results up telling me "Oops, that's not even in the ocean." When I use 4 for column and 4 for row.

I then delete -1 for both and it's ok to run it through and it works? I'm not understanding that piece of code why we use it in the random functions(randint) but it doesn't work in the if statement (range)?


#2

What is the lowest valid index and what is the highest valid index?
Figure out those two values, and let randint pick something in that range.

You probably just got lucky and didn't get the highest value, run it enough times and you'll get index out of range since you gave it an incorrect max valid index.


#3

with that -1 it seems to take away a row and col off.

Anything thing wrong with this and what is suggested ?

from 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:"))

# Write your code below!
if guess_row == ship_row and guess_col == ship_col:
    print "Congratulations! You sank my battleship!"

else:
    if guess_row not in range(0, len(board)) or \
    guess_col not in range(0, len(board)):
        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)

#4

What's the result after removing 1 from the length? Is it the highest valid index?
What if you don't remove 1? What happens if you use that as an index?


#5

Removing -1 in "range(0, len(board)-1)" lets me use row 4 and column 4, when using "Range(0, len(board))" instead of it telling me "Oops, that's not even in the ocean."

I don't know. The index/indices are the positions in the list right? for example the numbers in [0, 1, 2] well how does the -1 play into it? I figure it takes away a random/last row and column?

I found out I can just use "range(len(board))" (Taking the "0"(zero) away works)

All this confusion is from me not understanding the instructions and getting help on 12/19 by looking on here from other people trying to learn. I went here to get this: http://discuss.codecademy.com/t/12-bad-aim/11880

The instructions, hint on 12/19 and that discussion is what threw me off and still does.


#7

It actually doesn't. Keep in mind that randint() is inclusive. It means thatif you put randint(0,100) it will pick a random value from 0 to 100 including 100.


#8

See now I'm highly confused with this again. Even though I meant that -1 for range() part of code at the time, which I suppose I should've specified. You're saying that (random integer) randint(0, 100) but I see randint(0, len(board) -1) I thought it meant take board (board) from (start) "0," to (length)"len()" of board and take "-1" randomly. I would like to know why -1 and what does it do and why is it there?

Now with the range(0, len(board) -1) from experimenting in code academy labs. Taking the -1 away from that gave the result of what I was looking for and further more taking 0 away too.(It defaults at that range). With that -1 in the range code range(0, len(board)-1) it takes away a row and col. It does when I do it in code academy labs. What I would like to know is a break down of what each one does especially the -1?

Thank you!