While loop quiz

I feel like the explanations on the wrong answers do not provide enough insight as to why they are incorrect | correct. For example how do you get 32 on this, it does not explain how

x = 1
while ( x < 20 ):
x = x * 2
print(x)

Hi @riotch,

Refer to this post for better understanding of Python while loop.

Thanks,
Trung.

Hi @riotch

We just need the think about what the code is doing. This isn’t specifically a Python thing. All languages would have the same result.

Let’s iterate the loop manually, starting with x = 1, and also let’s format the code correctly. Indentation is part of the syntax!

x = 1
while (x < 20):
    x = x * 2
print (x)
  1. Is x < 20? Yes; x = x * 2 (2)
  2. Is x < 20? Yes; x = x * 2 (4)
  3. Is x < 20? Yes; x = x * 2 (8)
  4. Is x < 20? Yes; x = x * 2 (16)
  5. Is x < 20? Yes; x = x * 2 (32)
  6. Is x < 20? No (it’s now 32). while condition is now false, so go to the next line at the same indentation as the while statement

print (x) : 32

thank you for the reply, sorry I just copy pasted and didn’t indent. I still don’t understand where your getting (2) (4) (8) (16) (32) when all the lines are the same, how are you getting a different calculation? It appears the numbers are being doubled but how/why?

Let’s watch it in action in a debugger. Note the value of x in the left column increasing as we step through the program. The currently executing line (green bar) keeps going round the while loop until x is not less than 20:

py-debug

OK so i get that its printing on 32 which is greater than 20, but where are the increments of X coming from?

i think i got it, so 1 x 2 is 2 and 2 x2 is 4 and 4 x 2 is 8 etc

Correct - can you see that happening in the debugger?

Variable assignment and math in programming is exactly like algebra. You did y = x * 2 in math at school. Here we are assigning the result back to x instead of to y, so each time round we are doubling the value of x

* is the symbol for multiply in pretty much all programming languages. / is divide.

yes i can see it in the debugger. im interested to know how do I debug from linux command line? some of the if i statements are not making sense to me but i can run them and get the output but i would like to know the logic behind them

for example:

i = 5
while True:
… if i % 0o11 == 0:
… break
… print(i)
… i += 1

5
6
7
8

im not understanding the line - if i % 0o11 == 0:

If your machine has a GUI desktop, then you should install VSCode. A visual debugger is far superior to any command line one.

0o11 is an octal number, with a decimal value of 9 (1 in the 8’s column, and 1 in the 1’s column)

% is the modulo operator, ie. it yields the remainder of the division of the left value (i) by the right value (decimal 9)

So, to get remainder zero, the value of i must be exactly divisible by decimal 9.

Thus the program ends when i is 9, and the break makes it exit the while loop early before it prints 9.

To paste in code, press the </> icon in the toolbar above where you are typing. Then it will be correctly formatted

And look like this
1 Like

I installed vscode and used it as expected for this example

so you calculated 11 octal like this - 11 = (1 × 8¹) + (1 × 8⁰) = 9 decimal value

ok so to get remainder of == 0 the only decimal number would be 9 therefore it prints 1-8

and the i += 1 runs it over adding 1 until it reaches the break point of 9

i just need explanation on this one and how you get to 6

i = 1
x = 3
sum = 0 
while ( i <= x ):
 sum += i
 i += 1
print(sum)

Hi @riotch ,

I think you need to revisit the basic operations in Python like +, +=, -=, <= … before moving to the loop.
Here’s a good reference: Python - Basic Operators

Happy learning,
Trung.

i get that 1 is less than or equal to 3, then what? 0 += i(1) then i += 1 so 2 <=3 keep going and i get 4

Hi @riotch,

i = 1, i <= 3 is true
sum += 1 → sum = 1
increase i by 1 (1+=1)
i = 2, 2 <= 3 is true
sum += 2 → sum = 1 + 2 = 3
increase i by 1 (2+=1)
i=3, 3 <= 3 is true
sum += 3 → sum = 3 + 3 = 6
increase i by 1 (3+1)
i=4, 4 <=3 is false, stop the loop

What you need is practice more with the basic operations of Python, + - * / += -= > < >= <=, using VS Code or any IDE, write some function, debug it and see how it run.

Thanks,
Trung.

so 4 stops the loop which i got that far but the answer is 6, is that because of the sum or why? and im just following along in the training with 1 and 2 minute videos, no prior coding experience so ok on the extra resources to look at however im looking for the examples used in the course to be broken down to a place of understandable, they should have commented them.

Hi @riotch ,

You need to read the question carefully, if they asked for the sum value after the code is executed, it will be 6.
Btw, if you don’t have prior coding experience, it will be hard if you just READ the code, you need to WRITE it in IDE (vscode, sublime, etc…) and actually run it to see how things going, like the increment of variables like i and sum.
Programming is not easy at first, so be patient we’re here to support you.

Happy learning,
Trung.

1 Like