Hey guys,can you explain this to me?

Hi Simeon,

I might be able to explain to you, if I could see it, but in the email I
see only an obfuscated reference like this:

pp|690x498

Can you use words to describe what your question is please? If need be,
please copy and paste a minimal example of code?

If you are posting on the Discuss web site, I think you can use the
</> button in the rich text editor toolbar to format text as code. If
you are replying by email, you can surround your block of code with
lines containing three backticks like this:

```
code goes here
```

That should preserve formatting and prevent the Discuss software
removing the indentation and rearranging the code block.

so this is the code:

number = [5, 2, 5, 2, 2]

for x_count in number:
for count in range(x_count):
print(x_count)

so when I run it my console returns this:
5
5
5
5
5
2
2
5
5
5
5
5
2
2
2
2

and I cannot understand why

Well, we cannot see why you do not understand. What did you expect to be different?

I changed your code to the following:

number = [5, 2, 5, 2, 2]

for x_count in number:
    for count in range(x_count):
        print(x_count, "-", count)

And I got:

5 - 0
5 - 1
5 - 2
5 - 3
5 - 4
2 - 0
2 - 1
5 - 0
5 - 1
5 - 2
5 - 3
5 - 4
2 - 0
2 - 1
2 - 0
2 - 1

Does that help?

Hi Simeon,

What did you expect the code to do? It looks like it works fine to me.

Run through the code in your head, or using pencil and paper.

Your x_count variable starts with the value 5 (the first item of
“number”). So then you run through the inner loop which prints x_count
five times. That gives you 5 5 5 5 5.

Then your x_count takes the value 2. (The second value from “number”.)
So then you run through the inner loop and print x_count two times. The
gives you 2 2.

Then x_count takes the third value from “number”, which is 5 again. So
you run through the inner loop and get 5 5 5 5 5.

And so on, for the fourth value (2) and fifth value (2).

If you don’t understand what the code is doing, it can help you to put
extra calls to print in the code:

for x_count in number:
    print("outer loop: x_count is", x_count)
    for count in range(x_count):
        print("inner loop: x_count is", x_count, "; count is", count)
        print(x_count)

Guys, I see it looks so easy for you and there is no way I cannot understand that.
But I have just started to learn python and I watched less than 2 hours of the tutorial I found on youtube so even after your explanations I still cannot understand why It is printed 5 times.
I mean, I see that the first ‘for’ loop takes the first 5 then the second loop takes ‘‘count’’ in range, that means 5 becomes 0,1,2,3,4 and count is 0 or 1 or 2 or 3 or 4 - printed in order, but then I print ‘‘x_count’’ so why that range function even matters when I do not print ‘‘count’’ and even if it matters in a kind of way why is it five times 5 printed and not
0
1
2
3
4
I don’t know if I sound stupid or smth. I just started and I am just confused. Thank you if you going to explain it to me again, If not, thank you again for answering my answer anyway the first time.

From this I gather you very well understand what happens.

The range function matters because the code is supposed to be doing something else than printing count. It prints each number in numbers number times. 5 times 5 , 2 times 2 etc. If you want to print the count you write something else. Programming is not DWIM (Do What I Mean), the computer strictly follows what you write. So if you write print(x_count), that is what happens, if you mean to write print(count), that is fine, write that.

Hi Simeon,

If you tell the computer to print x_count, it will print x_count. It
will not guess that you actually wanted to print count and print that
instead.

Try this:

numbers = [5, 2, 7, 0, 1, 3, 4]
for number in numbers:
    print("outer loop: number is", number)
    for index in range(number):
        print("inner loop: number is", number)
        print("index is", index)

Using variable names that are clearly different can help you understand
your code. Changing the numbers can also help to see the pattern.

Okay, I assume, following your answers, that the range function influences the number of times that one number is printed. So okay we get:
0
1
2
3
4
And then when we print x_count why this
0
1
2
3
4
becomes 5 times 5.
We run the code and the program takes 5 first, enters the next loop, and notices that count is an item of the range function - 0,1,2,3,4. And then, what happens? Why do we get 5 five times!?

Simeon, I have twice posted some code that you should run. Have you run
it yet? You really should. It might help you.

Simeon wrote:

“We run the code and the program takes 5 first, enters the next loop,
and notices that count is an item of the range function - 0,1,2,3,4.”

No. The program doesn’t “notice” anything. You can change your code to
this:

for x_count in [500, 200, 500, 200, 200]:
    for count in range(x_count//100):
        print(x_count)

and it will print 500 five times, then 200 twice, then 500 five times,
then 200 twice, then 200 two more times.

“And then, what happens? Why do we get 5 five times!?”

You get 5 printed because that is the value of x_count, and you are
printing x_count. If x_count was 17, it would print 17. If x_count was
“Hello World”, it would print “Hello World”.

Your code says print(x_count) so it prints x_count, which is 5.

Why does it print five times? Because it is inside a loop. The loop runs
five times because you have range(x_count), and x_count is 5. If
x_count was 17, it would run 17 times. If x_count was 3, it would run
three times.

If you changed the loop to use range(10*x_count), you wouldn’t get
five times, you would get 50 times. If you used range(1000*x_count+2)
you would get 5002 times.

Let’s go through this step by step:

x_count = 4
for count in range(x_count):
    print("Hello")
    print(x_count)
    print(count)
    print("Goodbye")

How many times will “Hello” be printed?

How many times will “Goodbye” be printed?

How many times will 4 be printed?

How about the numbers 0, 1, 2, 3?

Once you have run that code and understood what it does, then you can
move on to the more complicated code:

for x_count in (2, 3, 4):
    for count in range(x_count):
        print("Hello")
        print(x_count)
        print(count)
        print("Goodbye")
1 Like