Python Forum
Confusing in [for loop] topic
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Confusing in [for loop] topic
#1
Hi,

I read a book with this below question and answer.
However I cant get the meaning of the explanation.

x = 0
n = 5
for i in range(1, n):
    for j in range(1, n):
        if((i+j) == 2):
            x = x + 2
        if((i+j) == 3):
            x = x + 3
        if((i+j) == 4):
            x = x + 4
print(x)
Output:
20
The explanation was:
First loop are 1,1 ; 1,2 ; 1,3.
Second loop are 2,1 ; 2,2.
Third loop are 3,1.
Fourth loop is nothing.
>>> So x = 2 + 3 + 4 + 3 + 4 + 4 = 20

Why there is nothing in the fourth loop? Huh
How is the logic of this loop ? Huh
Reply
#2
You're going to need to be clearer about what you mean. There are two loops there, not four. Trace through execution of the program by hand, writing down the values of the variables as you go. That will help you understand what's going on. You can also add extra calls to print to see what the values are.
Reply
#3
You are using the wrong term. Your program has two loops. The outer loop ITERATES 4 times: (0, 1, 2, 3). The inner loop iterates 16 times: 4x(0, 1, 2, 3). Some would also say (correctly) that the inner loop ITERATES 4 times and this is repeated 4 times.

Add a print statement to the loop:
x = 0
n = 5
for i in range(1, n):
    for j in range(1, n):
        if((i+j) == 2):
            x = x + 2
        if((i+j) == 3):
            x = x + 3
        if((i+j) == 4):
            x = x + 4
        print(i, j, i+j, x)
print(x)
Output:
1 1 2 2 <- Three changes while i = 1 1 2 3 5 <- 1 3 4 9 <- 1 4 5 9 2 1 3 12 <- Two changes while i = 2 2 2 4 16 <- 2 3 5 16 2 4 6 16 3 1 4 20 <- Only one change while i = 3 3 2 5 20 3 3 6 20 3 4 7 20 4 1 5 20 <- No changes while i = 4 4 2 6 20 4 3 7 20 4 4 8 20
Focusing on the last two columns you can see that the value of the last column (x) only changes when the value in the 3rd column (i+j) is 2, 3 or 4.

I would explain this as "x only increases if the value of i+j is 2, 3 or 4", but if you wanted to describe it using loops you could say:

When i is 1 you add values for j = 1, 2 and 3 (2, 3, 4)
When i is 2 you add values for j = 1 and 2 (3, 4)
When i is 3 you add values for j = 1 (4)
When i is 4 you don't add any values
x = 2 + 2*3 + 3*4 = 2 + 6 + 12 = 20
Sherine likes this post
Reply
#4
(Jul-31-2021, 11:23 AM)ndc85430 Wrote: You're going to need to be clearer about what you mean. There are two loops there, not four. Trace through execution of the program by hand, writing down the values of the variables as you go. That will help you understand what's going on. You can also add extra calls to print to see what the values are.

Thanks for your reply.
I'm new to Python, so I have a lot of confusing.
Reply
#5
(Jul-31-2021, 01:45 PM)Sherine Wrote: "i in range (1, n)"
the n = 5,
so why the loop is not going until 4? it's only 3.

How are you determining that the loop only goes up to 3 and not 4? Have you printed out the value of i on each iteration? Show us that output; it should be going to 4.
Reply
#6
From the documents:

https://docs.python.org/3/library/stdtypes.html#range

Quote:class range(start, stop[, step])
The arguments to the range constructor must be integers (either built-in int or any object that implements the __index__ special method). If the step argument is omitted, it defaults to 1. If the start argument is omitted, it defaults to 0. If step is zero, ValueError is raised.

For a positive step, the contents of a range r are determined by the formula r[i] = start + step*i where i >= 0 and r[i] < stop.
In your example the range starts with i = 1 and continues while i < 5. Since it is not specified the step size defaults to 1. This results in i having values [1, 2, 3, 4].
print(list(range(1, 5)))
Output:
[1, 2, 3, 4]
Sherine likes this post
Reply
#7
(Jul-31-2021, 01:41 PM)deanhystad Wrote: You are using the wrong term. Your program has two loops. The outer loop ITERATES 4 times: (0, 1, 2, 3). The inner loop iterates 16 times: 4x(0, 1, 2, 3). Some would also say (correctly) that the inner loop ITERATES 4 times and this is repeated 4 times.

Add a print statement to the loop:
x = 0
n = 5
for i in range(1, n):
    for j in range(1, n):
        if((i+j) == 2):
            x = x + 2
        if((i+j) == 3):
            x = x + 3
        if((i+j) == 4):
            x = x + 4
        v
print(x)
Output:
1 1 2 2 <- Three changes while i = 1 1 2 3 5 <- 1 3 4 9 <- 1 4 5 9 2 1 3 12 <- Two changes while i = 2 2 2 4 16 <- 2 3 5 16 2 4 6 16 3 1 4 20 <- Only one change while i = 3 3 2 5 20 3 3 6 20 3 4 7 20 4 1 5 20 <- No changes while i = 4 4 2 6 20 4 3 7 20 4 4 8 20
Focusing on the last two columns you can see that the value of the last column (x) only changes when the value in the 3rd column (i+j) is 2, 3 or 4.

I would explain this as "x only increases if the value of i+j is 2, 3 or 4", but if you wanted to describe it using loops you could say:

When i is 1 you add values for j = 1, 2 and 3 (2, 3, 4)
When i is 2 you add values for j = 1 and 2 (3, 4)
When i is 3 you add values for j = 1 (4)
When i is 4 you don't add any values
x = 2 + 2*3 + 3*4 = 2 + 6 + 12 = 20

Thank you for your reply.
I understood with your statement
print(i, j, i+j, x)
,
this output make me clear.

This "x = 2 + 2*3 + 3*4 = 2 + 6 + 12 = 20" calculation I can't understand. Think
But it's okay, I really appreciate to your statement, let me know how this statement work.

Thanks Smile
Reply
#8
(Jul-31-2021, 01:51 PM)deanhystad Wrote: From the documents:

https://docs.python.org/3/library/stdtypes.html#range

Quote:class range(start, stop[, step])
The arguments to the range constructor must be integers (either built-in int or any object that implements the __index__ special method). If the step argument is omitted, it defaults to 1. If the start argument is omitted, it defaults to 0. If step is zero, ValueError is raised.

For a positive step, the contents of a range r are determined by the formula r[i] = start + step*i where i >= 0 and r[i] < stop.
In your example the range starts with i = 1 and continues while i < 5. Since it is not specified the step size defaults to 1. This results in i having values [1, 2, 3, 4].
print(list(range(1, 5)))
Output:
[1, 2, 3, 4]

Thanks for your document link.
Reply
#9
(Jul-31-2021, 01:47 PM)ndc85430 Wrote:
(Jul-31-2021, 01:45 PM)Sherine Wrote: "i in range (1, n)"
the n = 5,
so why the loop is not going until 4? it's only 3.

How are you determining that the loop only goes up to 3 and not 4? Have you printed out the value of i on each iteration? Show us that output; it should be going to 4.

Since the quiz explanation are
1,1 ; 1,2 ; 1,3
2,1 ; 2,2
3,1
so I just wondering why no 4 in this explanation.

However, now I clear with that already.
Thanks for your reply.
Reply
#10
When i is 1 you add values for j = 1, 2 and 3 (2, 3, 4)
When i is 2 you add values for j = 1 and 2 (3, 4)
When i is 3 you add values for j = 1 (4)
When i is 4 you don't add any values

x = (2 + 3 + 4) + (3 + 4) + (4)
x = 2 + 3 + 4 + 3 + 4 + 4
x = 2 + 3 + 3 + 4 + 4 + 4
x = 2 + 2 * 3 + 3 * 4
Sherine likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [split] New thread/topic rajp1497 1 1,878 Sep-24-2020, 01:55 AM
Last Post: micseydel
  subscribing to kafka topic/key georgelza 10 4,242 Jan-03-2020, 04:58 AM
Last Post: georgelza
  Confusing logic Blob 4 2,400 Nov-18-2019, 03:26 AM
Last Post: Blob
  How do I create a actor if I subscribe to a specific topic? sdf1444 0 1,696 Aug-01-2019, 09:29 PM
Last Post: sdf1444
  Help with finding correct topic in Python learning yahya01 1 2,195 Jun-06-2019, 05:01 PM
Last Post: buran
  Confusing output from 2to3 about what files need change Moonwatcher 1 4,825 Dec-30-2018, 04:07 PM
Last Post: Gribouillis
  Topic Modelling - Document Labels Nicson 0 1,710 Nov-20-2018, 04:56 PM
Last Post: Nicson
  IndentationError message could be confusing to new programmers insearchofanswers87 1 2,342 May-16-2018, 05:05 PM
Last Post: Larz60+
  Confusing Math DrJu 2 3,233 Jan-18-2018, 10:47 PM
Last Post: Windspar
  Some Confusing Program Errors (Newbie stuff) WildPictus 1 2,788 Sep-03-2017, 05:00 PM
Last Post: hbknjr

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020