3/19

I have no idea how to use a for loop to create the type of board they want me to…
I’m assuming they want me to create a board that looks 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’

Now, I’ve been to the forums and seen that other people have had a lot of trouble with this particular exercise as well, and they usually end up with some form of this code:

board =
for x in range(0,5):
Circles = [“O”] * 5
board.append(Circles)
print board

This is all fine, I understand this code, but when I save and submit it, I get this output:

[[‘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’]]

So… what am I missing

if you have “print board” in the loop itself it goes wrong, you should put it outside the loop if that’s your problem

2 Likes

I agree with redmaddic, its outside of the loop print needs to be. Here is what worked for me:

board = []
 for x in range(0,5):
    circles = ["O"]*5
    board.append(circles)
print board
5 Likes

Thanks, both of you, it works now. Can anyone explain why it needs to be outside the loop?

1 Like

if you have it inside the loop it will keep doing the “print board” function for as long as the loop plays. so if its outside of the loop it will only play it once

1 Like

What does x represent in this code? It isn’t a variable initialized elsewhere in the code block (like board or circles)… It isn’t a recognized command (like for… range() or .append())… And it isn’t referenced or called anywhere else in the code block… How does the program know what to do with it when executing?

just think of it as a place holder for your loop, as far as i know it just holds the value every time you loop i;e if your range is 1 to 10. it will loop through 10 times and x = 1 x = 2 x=3 and so on. but i am new too so i might be wrong.

this did not help mkkkjk.uglniki,jkljml/kl/klk/

It seems to me that for begginer its hard to inderstand that the word "circles" is needed here. Thats why I have this ant it`s okey:
board = []
for x in range(0,5):
O = [“O”] * 5
board.append(O)
print board

I don’t understand why you need “circles” at all. What is it doing in this piece of code? I wrote the following, and it worked:

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

Very confused. Are there simply two ways to do this? Or is one way more sound than the other?

1 Like

Hey sardawson,

It’s just two ways to get the same output. Depending on the goals of the code both work. For this lesson your code is more efficient though.

1 Like

Hey there,

can anyone explain where the (0,5) comes from?

Hi, methodrunner20362!

With the range() method, (0,5) indicate the start and length of the range. The 0 is where the count will begin, and the 5 (not inclusive) is where it will stop.

So if you had the following code:

for i in range(0,5):
    print i

You would end up with:
0
1
2
3
4

Hope that helps!

1 Like

Hi there,
I used the same code as you, but i’m still confused when I should use ( ) and when . the same thing is with ’ 'or " ". I got it that strings should be in " ". lists in , dictionaries in { } but sometimes is realy confusing. I guess in this excercise I’ve been lucky to guess it, but I would like to know if there are some rules about it

thanks in advance
Vladana

Hey dada1506,

Unfortunately, you’ve struck on programming syntax with that one. The short answer is we’ve all got to memorize programming syntax just like we’ve learned (or attempted to in my case) English grammar. I can’t get into why , {}, and () were chosen when the language was developed, but they are absolutely important for proper use.

So far, the only time you’ll typically see is when you are defining an empty list like so:

board=

Making a list that already has data, like this:

board=[“O”,“O”,“O”,“O”,“O”]

or using a loop to build a list, like so:

board.append([“O”]*5)

This last one appends [“O”,“O”,“O”,“O”,“O”] as a new list inside of the list board.

Also, so far the only time you’ll see {} is when defining a dictionary. We haven’t played with those since Student Becomes the Teacher though, so I think we’re all getting a bit rusty on those.

The one we’ve been using a TON though is (), which is used for a bunch of things. Generally, you’ll see () when defining variables for a function, like so:

for i in range(0,5):

They are also used when passing information into a method like .append:

board.append([“O”] * 5)

Finally, you’ll also see them used for typical math functions, but you’ll likely be comfortable with them in that use case.

As for the last item, use of “O” vs. ‘O’ it’s mostly personal choice for the stuff we’re doing now and could be considered interchangeable. That said, they are treated subtly differently when you need to escape special characters and such in text or other code. This is a good resource on those differences:

http://henry.precheur.org/python/python_quote.html

1 Like

codyroche,

treat info you gave me. It’s more clear now, how should I use it, or at least if I should pay attention to it or not.
Thanks so much for your help !

Dada