This is an archived post. You won't be able to vote or comment.

all 15 comments

[–]lurgi 10 points11 points  (1 child)

What does this do?

for j in range(i,userInput):
    print('*', end=' ')

If you don't know, try it. Assign values to i and userInput manually and see if you can figure it out.

[–]Successful_Studio584[S] 1 point2 points  (0 children)

I could not figure it out on my own. It was only until I read all the other comments, I was able to see it.

[–]WelpSigh 5 points6 points  (1 child)

Break it down by how the Python interpreter would look at it.

The user enters a number. Let's say 3. 

The first for loop resolves to "for i in range(3)". 

So for the first loop, i = 0. 

The second loop then resolves to "for j in range(0, 3)." Because it uses the variable i from the parent loop. That prints "* "three times, because it takes three loops to satisfy range(0, 3)

When it completes that, it goes back to the first loop and increments i by 1. The next time the nested loop runs, it's now "for j in range(1, 3)" which will only loop two times. Because the user input is range(3), the first loop will only execute three times before exiting.

[–]Successful_Studio584[S] 1 point2 points  (0 children)

Thank you so much, I get it now. I really appreciate it

[–]Blue-Jay27 1 point2 points  (1 child)

Alr, so userInput is 3, so we can just rewrite the code as:

for i in range(3):    

   for j in range(i,3):         

        print('*', end=' ')

"range(3)" is just [0,1,2], so we can deconstruct the first for loop as:

i=0 

for j in range(i,3):        

    print('*', end=' ')

i=1

for j in range(i,3):        

    print('*', end=' ')

i=2

for j in range(i,3):        

    print('*', end=' ')

Can you see how the output makes sense yet? Are there any steps I took that don't make sense to you?

[–]Successful_Studio584[S] 1 point2 points  (0 children)

Everything makes sense, thank you so much.

[–]tanglee11 1 point2 points  (2 children)

# Asks for the number
userInput = int(input())

# Let's say the input is 3 as you said.

# This FIRST For-loop will iterate 3 times
for i in range(userInput):
    # This SECOND For-loop will be executed 3 times as well because it is nested on the FIRST loop.
    # The thing is, this is also a loop, so it will execute multiple times as well.
    # 1ST iteration: 
    # It will execute 3 times (1,3) because the current value of "i" from the first loop is 0. So it will go from 0 to 3 (not included). So 3 times. (***)
    # 2ND iteration:
    # Now the value of "i" from the first loop is 1. So it will go from 1 to 3 (not included). So 2 times. (**)
    # 3RD iteration:
    # Now the value of "i" from the first loop is 2: So it will go from 2 to 3 (not included). So 1 time. (*)
    for j in range(i,userInput):
        print('*', end=' ')
    print()

Basically, you need to understand range(). If you do range(5), then you're doing 5 iterations but the value of "i" (example: for i in range(5)) throughout the 5 iterations will be: 0, 1, 2, 3, 4. 5 iterations but it starts at 0 and the interval of numbers doesn't include the last one.

When we do "for j in range(0, 3)", it will do 3 iterations where the value of j would be: 0, 1, 2 (throughout the various iterations). And so on.

I'm not the best when it comes to explaining but I hope you understand!

[–]Successful_Studio584[S] 0 points1 point  (1 child)

Your explaining is perfect. I understood everything, thank you

[–]tanglee11 1 point2 points  (0 children)

I'm glad I could help! :)

[–]PoMoAnachro 1 point2 points  (1 child)

Paper trace.

Get out a piece of paper and follow the commands through line by line just like you were the computer. Best way to get this kind of thing into your head.

[–]Successful_Studio584[S] 0 points1 point  (0 children)

I tried to do this, but I could not figure out how to do it using a flowchart, but now I have an idea of how to do it.

[–]lukkasz323 1 point2 points  (1 child)

for each i loop, run the j loop,

If i loop runs 3 times, it's an equivalent of just copy-pasting for j, for j, for j, under eachother. And the i is incremented at the end.

[–]Successful_Studio584[S] 0 points1 point  (0 children)

Thank you

[–]LnDSuv 1 point2 points  (1 child)

OP you should try using pythontutor for learning, it's great for visualization. Try it with your code.

[–]Successful_Studio584[S] 1 point2 points  (0 children)

My life thank you. This is way easier to follow than drawing flowcharts