I don't understand , help me please


#1



https://www.codecademy.com/en/courses/python-beginner-en-4XuFm/0/3?curriculum_id=4f89dab3d788890003000096


hi guys , thx for helping , i am trying to learn python , but here , i don't understand something , the system of for (for i in range(5) ) in this exercice , can someone explain me what it does that in the console :

[['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']]

how is it link to board.append(["O"] * 5) ?

thanks


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


#2

You're printing board after each time you append another row to it.
Perhaps you'd rather only print it once, after you've finished adding the rows.


#3

hum , yeah , but why does it append another row ? why i and range(5) a linked to what is next ? (board.append(["O"] * 5))


#4

What do you mean by another row?

What do you expect to happen and what happens instead?
Try to make it very clear what you mean.


#5

i don't understand the system of the "for i in range(5): " , i don't understand the lonk between that and "board.append(["O"] * 5)"

in clear , why :

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

can do that :

[['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']]


#6

The code you posted does not run due to its incorrect indentation.

If you mean this:

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

Then that does not produce any output to the console.

English might not come easy to you, and that is okay. But that means that you have to be extra careful with everything that you say, or you'll end up with something that requires so much guessing that it becomes very difficult to understand!


#7

well when i do :

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

it print that in the console :
[['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']]


#8

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

Will have that output. Yes.

Still not sure what you're asking. If you're wondering what the difference is, compare the code!

If you're wondering why it prints more than just 5x5, then I believe I already answered that in my first post.


#9

yeah , but my question is why" for i in range(5) " for exemple the i and range(5) are linked with board.append(["O"] * 5) ?
what does python calculate ? range(5) * board.append(["O"] * 5) ?


#10

A loop causes code to run more than once.
range(5) returns a list, [0, 1, 2, 3, 4] and if you iterate through that list you get 5 iterations.


#11

so it should do :

[['O', 'O', 'O', 'O', 'O']]

and not

[['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']]


#12

I don't know what you want it to do!

I believe that the intention of the exercise is to add 5 rows, each containing 5 'O's
Which would mean adding a row 5 times. And then if you want to print it, you'd do that after having finished adding rows.


#13

here come my question :smile:

when you write range(5) you ask it to do 5 rows ?


#14

No, all that does is create a list:

>>> range(5)
[0, 1, 2, 3, 4]

range is a function that creates a list containing a range of numbers based on the arguments. When the argument is 5, you get a list starting at 0 and counting up to, but not including 5.

Documentation for range can be found here:
https://docs.python.org/2/library/functions.html#range

for i in [0, 1, 2, 3, 4]:
    print i

produces the following output:

0
1
2
3
4