Conditional Statement Practice-----Problem 16 -------2 distinct number whose value=100

Screen Link:
https://app.dataquest.io/m/1010/conditional-statements-practice-problems/16/finding-the-pair

I don’t understand the working of the solution code (2 for loop )
My Code:

values = [72, 50, 48, 50, 7, 66, 62, 32, 33, 75, 30, 85, 6, 85, 82, 88, 30, 32, 78, 39, 57, 96, 45, 57, 61, 10, 62, 48, 32, 96, 75, 15, 50, 50]
value1 = None
value2 = None
for x in values:
    for y in values:
        if x + y == 100 and x != y:
            value1 = x
            value2 = y

What I expected to happen:
In the first iteration, I expect, x=72, y=72. in the second x=50, y=50. So essentially both x and y have the same values. Am I missing something?

What actually happened:

Replace this line with the output/error

You have a nested for loop here. Both of them don’t run simultaneously.

  • First for loop, iteration 1: x = 72.

    • Second for loop, iteration 1: x = 72, y = 72
    • Second for loop, iteration 2: x = 72, y = 50
    • Second for loop, iteration 3: x = 72, y = 48
    • and more iterations
    • Second for loop, last iteration: x = 72, y = 50
  • First for loop, iteration 2: x = 50

    • Second for loop, iteration 1: x = 50, y = 72
    • Second for loop, iteration 2: x = 50, y = 50
    • Second for loop, iteration 3: x = 50, y = 48
    • and more iterations
    • Second for loop, last iteration: x = 50, y = 50
  • First for loop, iteration 3: x = 48, and so on.

For every iteration of the outer loop, the inner loop will go through all of its iterations.

1 Like

Thank you so much! That’s really very helpful!! :grinning_face_with_smiling_eyes: