Can not create a 5x 5 grid (have used all recomendations from this forum)


#1

Hi Experts,

trying to generate 5 x 5 grid.

I have searched the forum and found following solution:

board = []

for i in range(5):
     board.append(["0"]*5)

print board

But it gives me:
[['0', '0', '0', '0', '0'], ['0', '0', '0', '0', '0'], ['0', '0', '0', '0', '0'], ['0', '0', '0', '0', '0'], ['0', '0', '0', '0', '0']]

Would you please help me to figure out what wrong is ?
BTW -i think this is correct behavior but many posts on this forum say it is the answer to create 5 x5 grid.


#2

The board is a list. What would happen if you printed each list inside the list so each one is on a new line?


#3

If i print every iteration:

board = []

for i in range(5):
     board.append(["0"]*5)
     print board

I get:

[['0', '0', '0', '0', '0']]
[['0', '0', '0', '0', '0'], ['0', '0', '0', '0', '0']]
[['0', '0', '0', '0', '0'], ['0', '0', '0', '0', '0'], ['0', '0', '0', '0', '0']]
[['0', '0', '0', '0', '0'], ['0', '0', '0', '0', '0'], ['0', '0', '0', '0', '0'], ['0', '0', '0', '0', '0']]
[['0', '0', '0', '0', '0'], ['0', '0', '0', '0', '0'], ['0', '0', '0', '0', '0'], ['0', '0', '0', '0', '0'], ['0', '0', '0', '0', '0']]

This i understand


#4

You're still printing the whole board each time you iterate up to 5. You should iterate through "board" after it's fully written, and then print each iteration, not the whole board. If you're on the lesson I think you are, within the next two lessons, you will set up a function that iterates through board and prints each line accordingly to make a grid.


#5

yea, then see the list you just appended to board, but if you now would make a loop to printed the nested lists:

board = []

for i in range(5):
     board.append(["0"]*5)

for i in board:
    print i

you get each list on a seperate list, if you now would use " ".join() you would get a string instead, which gives a nice representation of your board, you will do this in later exercise


#6

Thank you Guys,

ok, i was confused - i thought this code already creates a grid which i could not understand why.

Thank you once again.


#7

2 posts were split to a new topic: Making the board


#8

Its quite hard in this lesson to know if your doing stuff right or not. It just says "print the board" and then you do that and it looks like crap and you get the feedback "Way to go!". Maybe add a comment about the set-up looks strange "but we'll get to that later!"?


#9

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

if guess_row == ship_row and guess_col==ship_col:
    print "Congratulations! You sank my battleship!"
else:
    print "You missed my battleship!"
    for i in range(0, 5):
        for j in range(0, 5):
            board[i][j]="X"
        
    print_board(board)

What is the problem with that code Iam on Hit or miss Danger on Robisson it throws an error that iam updating guess row and col row -1 not the actual ones plz help i am bored of this :disappointed:
also mention what is meant by Set the list element at guess_row, guess_col to "X".
i set the list element to x in my else statement i dun understand the rest that maybe the problem so i need the help of you plz
error: Oops, try again. You're updating 'guess_row - 1' and 'guess_col - 1' instead of 'guess_row' and 'guess_col'.


#10

this bit of code:

for i in range(0, 5):
        for j in range(0, 5):
            board[i][j]="X"

why? this will make it look like the user guessed all fields, and all are wrong. You simply update one field in the board: board[x][y], x and y are not really what you need to use, you needed to update the guessed field, which variable hold this?


#11

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