Https://www.codecademy.com/courses/learn-python/lessons/battleship/exercises/check-it-twice?action=lesson_resume


#1

https://www.codecademy.com/courses/learn-python/lessons/battleship/exercises/check-it-twice?action=lesson_resume

When I print the code, why does the result in the console(black screen) become the way it is? I don’t understand how did it produce more lists like this[[‘O’, ‘O’, ‘O’, ‘O’, ‘O’]]
[[‘O’, ‘O’, ‘O’, ‘O’, ‘O’], [‘O’, ‘O’, ‘O’, ‘O’, ‘O’]]
[[‘O’, ‘O’, ‘O’, ‘O’, ‘O’], [‘O’, ‘O’, ‘O’, ‘O’, ‘O’], [‘O’, ‘O’, ‘O’, ‘O’, ‘O’]]
[[‘O’, ‘O’, ‘O’, ‘O’, ‘O’], [‘O’, ‘O’, ‘O’, ‘O’, ‘O’], [‘O’, ‘O’, ‘O’, ‘O’, ‘O’], [‘O’, ‘O’, ‘O’, ‘O’, ‘O’]]
[[‘O’, ‘O’, ‘O’, ‘O’, ‘O’], [‘O’, ‘O’, ‘O’, ‘O’, ‘O’], [‘O’, ‘O’, ‘O’, ‘O’, ‘O’], [‘O’, ‘O’, ‘O’, ‘O’, ‘O’], [‘O’, ‘O’, ‘O’, ‘O’, ‘O’]]

I thought the board variable would equal [‘O’, ‘O’, ‘O’, ‘O’, ‘O’]. Shouldn’t board.append([‘O’] * 5) be adding 5 ‘O’ 's to board list?


#2

your board is a 5x5 grid, [‘O’, ‘O’, ‘O’, ‘O’, ‘O’] is a single row. So that is why we use a loop to append 5 rows (['O'] * 5) to board

It seems (can’t confirm since you didn’t post your code) you placed print board inside the loop, so you the build of the board. After the first iteration of the loop, the board will be printed (containing one row), then the second iteration it will print the board again (two rows, given another row was added) and so on

if you only want the final 5x5 board, place print board outside the loop


#3

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