![]() |
Confusing in [for loop] topic - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Confusing in [for loop] topic (/thread-34440.html) Pages:
1
2
|
Confusing in [for loop] topic - Sherine - Jul-31-2021 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) 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? ![]() How is the logic of this loop ? ![]() RE: Confusing in [for loop] topic - ndc85430 - Jul-31-2021 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.
RE: Confusing in [for loop] topic - deanhystad - Jul-31-2021 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) 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 RE: Confusing in [for loop] topic - Sherine - Jul-31-2021 (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 Thanks for your reply. I'm new to Python, so I have a lot of confusing. RE: Confusing in [for loop] topic - ndc85430 - Jul-31-2021 (Jul-31-2021, 01:45 PM)Sherine Wrote: "i in range (1, n)" 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.
RE: Confusing in [for loop] topic - deanhystad - Jul-31-2021 From the documents: https://docs.python.org/3/library/stdtypes.html#range Quote:class range(start, stop[, step])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)))
RE: Confusing in [for loop] topic - Sherine - Jul-31-2021 (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. 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. ![]() But it's okay, I really appreciate to your statement, let me know how this statement work. Thanks ![]() RE: Confusing in [for loop] topic - Sherine - Jul-31-2021 (Jul-31-2021, 01:51 PM)deanhystad Wrote: From the documents: Thanks for your document link. RE: Confusing in [for loop] topic - Sherine - Jul-31-2021 (Jul-31-2021, 01:47 PM)ndc85430 Wrote:(Jul-31-2021, 01:45 PM)Sherine Wrote: "i in range (1, n)" 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. RE: Confusing in [for loop] topic - deanhystad - Jul-31-2021 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 |