19 Jan 2024

Posted on:

18 Jan 2024

0

# Question 9, Section 6: How does 'j' work?

I need help understanding Giles' solution to print the F. I am not understanding why "j == 1" works. I played around with the code a bit to see if I could understand its function a bit more and here is what I've found:
Changing any or all of the 1's in 'j == 1' to any number from 1-5 yields the same result (the F prints correctly). Changing them to 0 or 6 messes up the F entirely. I believe this is due to j's range(1,6), but why do these changes affect the output the way that they do? Is there a reason to choose the 1 over choosing 2-5 if they all give the same output?

I've posted an example of my playing with the code to show what I mean

5 answers ( 0 marked as helpful)
Instructor
Posted on:

19 Jan 2024

0

Hey Lae,

Thank you for reaching out!

Let's analyze the code step by step. The first line in the nested loop is:

``for i in range(1,7):``

This line iterates `i` through the values `(1, 2, 3, 4, 5, 6)`.

Following this, we have:

``for j in range(1,6):``

Here, `j` takes on the values `(1, 2, 3, 4, 5)`. The nested loop executes 30 times in total, cycling through combinations of `i` and `j` values in sequence:
`i = 1, j = 1`
`i = 1, j = 2`
`i = 1, j = 3`
`i = 1, j = 4`
`i = 1, j = 5`
`i = 2, j = 1`
`i = 2, j = 2`
`...`
`i = 6, j = 4`
`i = 6, j = 5`

The first condition in our nested loop is:

``if i == 1 and j < 6:``

This checks whether `i` is `1` and `j` is less than `6`. If true, a star is printed. Since this condition is met five times in succession (for each value of `j` from `1` to `5` when `i` is `1`), five stars are printed in a row.

Instructor
Posted on:

19 Jan 2024

0

After completing the first row, the loop proceeds with `i = 2`, `j = 1`. The next condition is:

``elif i == 2 and j == 1:``

This evaluates whether `i` equals `2` and `j` equals `1`. When this condition is met, a newline character is printed, creating a line break. The other combinations where `i` equals `2` are the following:
`i = 2, j = 2`
`i = 2, j = 3`
`i = 2, j = 4`
`i = 2, j = 5`
They are not considered in the subsequent code, meaning they don't meet any specified condition and hence, nothing is printed for those cases.
However, if we change this condition to

``elif i == 2 and j == 2:``

then a star will be printed only when `i = 2` and `j = 2`, ignoring all other combinations of `i = 2` with different `j` values, namely:
`i = 2, j = 1`
`i = 2, j = 3`
`i = 2, j = 4`
`i = 2, j = 5`

Instructor
Posted on:

19 Jan 2024

0

And that is the reason why other values of j also solve the problem. Applying similar logic to the remaining conditions, you can trace how the letter 'F' is formed.

Great job in figuring out that there exist other conditions that contribute to printing the letter! Don't hesitate to reach out if any part of this explanation remains unclear or if you have more questions.

Kind regards,

365 Hristina

Posted on:

19 Jan 2024

0

Oh my goodness, yes! This makes so much sense now. Thank you!

I think I wasn't understanding because I kept thinking what was printing for the F was printing a certain amount of times once the condition is met, rather it is printing it once for each iteration where the condition was true. Got it! Thank you🙏🏾

Instructor
Posted on:

19 Jan 2024

0

Hey Lae,

You are welcome, happy to help!

Best,

365 Hristina