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


Messages In This Thread
Confusing in [for loop] topic - by Sherine - Jul-31-2021, 11:17 AM
RE: Confusing in [for loop] topic - by ndc85430 - Jul-31-2021, 11:23 AM
RE: Confusing in [for loop] topic - by Sherine - Jul-31-2021, 01:46 PM
RE: Confusing in [for loop] topic - by ndc85430 - Jul-31-2021, 01:47 PM
RE: Confusing in [for loop] topic - by Sherine - Jul-31-2021, 02:17 PM
RE: Confusing in [for loop] topic - by deanhystad - Jul-31-2021, 01:41 PM
RE: Confusing in [for loop] topic - by Sherine - Jul-31-2021, 02:11 PM
RE: Confusing in [for loop] topic - by deanhystad - Jul-31-2021, 01:51 PM
RE: Confusing in [for loop] topic - by Sherine - Jul-31-2021, 02:15 PM
RE: Confusing in [for loop] topic - by deanhystad - Jul-31-2021, 02:33 PM
RE: Confusing in [for loop] topic - by Sherine - Jul-31-2021, 02:37 PM
RE: Confusing in [for loop] topic - by deanhystad - Jul-31-2021, 02:53 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [split] New thread/topic rajp1497 1 1,957 Sep-24-2020, 01:55 AM
Last Post: micseydel
  subscribing to kafka topic/key georgelza 10 4,484 Jan-03-2020, 04:58 AM
Last Post: georgelza
  Confusing logic Blob 4 2,505 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,759 Aug-01-2019, 09:29 PM
Last Post: sdf1444
  Help with finding correct topic in Python learning yahya01 1 2,283 Jun-06-2019, 05:01 PM
Last Post: buran
  Confusing output from 2to3 about what files need change Moonwatcher 1 4,906 Dec-30-2018, 04:07 PM
Last Post: Gribouillis
  Topic Modelling - Document Labels Nicson 0 1,776 Nov-20-2018, 04:56 PM
Last Post: Nicson
  IndentationError message could be confusing to new programmers insearchofanswers87 1 2,408 May-16-2018, 05:05 PM
Last Post: Larz60+
  Confusing Math DrJu 2 3,344 Jan-18-2018, 10:47 PM
Last Post: Windspar
  Some Confusing Program Errors (Newbie stuff) WildPictus 1 2,856 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