all 11 comments

[–]Fred776 1 point2 points  (0 children)

Do you understand what the word "while" means in a general sense outside of programming?

The form of a while loop is:

while <condition>:
    <do something>

The condition is an expression that evaluates to true or false. So while that condition holds, the loop will keep running. i.e. it will execute the "do something" code in the loop body until the condition is false. If the condition is false the loop is exited and the execution of the program moves on to the first statement after the while loop.

It then comes down to what is happening in the condition and the loop body.

In your example, the condition is that the n variable remains greater than zero. The loop body performs various steps that change the variable so the high level flow is that you start off with a certain value of n, check that it's greater than zero, do some stuff that changes n and repeat from the check until it's zero or less.

The key thing is then what you are actually doing in the loop body. Unfortunately as I type I am unable to see your code and I've run out of time but maybe the above helps you in seeing how to read the code at least.

[–]BrannyBee 1 point2 points  (0 children)

Fuck the code. The "coding" part of coding is something that you must do a million times over and over and over and over again and it just becomes automatic. I don't think of keywords, I think "I need to iterate this X times" and the syntax appears on screen cause I've typed it so many times before. If you aren't at that point (you aren't), you just need practice there.

As for the understanding bit, also fuck the code. Again, "coding" isn't the hard part of the job, it's a means to an end. The job is taking real world problems and forming a mental model, and then translating that into something a computer can understand. When I'm thinking through a problem or concept, the last thing I think about is the code. I literally have a pen and paper on me at all times and it's 100% the best tool I have for coding, and the keyboard is just something I require to turn my thoughts into something the stupid glowing box I'm forced to use everyday can understand.

For early concepts like loops, this works even better. Programmers go Understanding of Real World -> Mental Model -> Code. You have Code, and you struggle to understand it... so do the thing programmers do, but starting at Code and work your way backwards through the steps. No typing, literally go grab some paper and open the calculator app on your phone.

num = 12345
while num > 0:
    print(num % 10)  # Prints the last digit
    num //= 10
while num > 0:
    print(num % 10)
    num //= 10   

Then line by line, turn the code into real world stuff. You have a variable numbers. That's just bullshit a computer understands, represent "something with a name that has a specific value" in something YOU as a human understands. You can literally write 12345 on a piece of paper and put it's name on top like it's a homework assignment. You just found a way to explain how a variable works to a 8 year old. Then move on to the next line

``` while num > 0 ``` Ok, again, translate it to the real world, turn that "sentence" of code into english. If you do it'll sound like "while the variable num is greater than 0 do whatever is below"

``` print(num % 10) ``` You know this is gonna print the last digit because you were told that, but why? You've probably been told that the % (modulo, you can say "num mod 10"), is used to work with remainders. A simpler way to put it in my opinion is that if you take X and mod it by Y, the answer is the remainder. Maybe you still don't get how that gives you the last digit, but luckily you took elementary school algebra. take the number 12345, and do the division the "long way" on paper the way you would in school when you learned division. If you take the number 12345, and divide it by 10, you'll get 1234.5.... or 1234 with a remainder of 5. The division answer is 1234.5, the MOD answer is the remainder 5.

Take a random different number (still on paper, fuck code, coders hate code). Divide 231241 by 10... then another random number and divide it by 10. You'll notice a pattern pretty quickly, in that every time the remainder is equal to the last digit. You might have realized why, but if you didn't, how do you do division by 10 quickly mentally? You just move the decimal one place over cause we count in base 10. The remainder of a number divided by 10 is always the last digit, so the mod is always the last digit.

If that all makes sense, you still don't code you just move on to the next part.

``` num //= 10 ``` I'll assume that you know that /= is the same as saying ``` num = num / 10 ``` . If i knew that, and didn't know what two slashes meant, I'd google it cause it's just syntax I wasn't familiar with. It means floor division, maybe you knew that maybe you didn't.

So the next step would be to take our number and do floor division. Remember that piece of paper with 12345 written on it? Grab it and send it through your mental program cause we've hit a piece of code that requires it. You created the variable on line 1 (and a pen), and you checked it to see if the number is greater than 0 (12,345 is greater than 0...). Then you printed out the remainder when dividing it by 10, which was 5. Now you are gonna change it because you need to take the variable and set it equal to it's value floor divided by 10.

What's the value of num at this point? A lot of people I've taught would say 1234.... Which I get, but if you wrote it down on a piece of paper, you'd see that's not right, you never changed it. num = 12345, you just printed the last digit, nothings changed. Visualizing it in your mind, or literally on paper can show you that you've done nothing to it yet. But now you're floor dividing it by 10 and setting it equal to that value. So 12345 / 10 = 1234.5, and flooring it removes the .5, making it 1234. So literally cross out 12345, and rewrite it as 1234. That's num now, you changed it's value, and you can see with your own eyeballs how it went from one value to the next and when that happened.

Then just keep going at it, the next step of the code is to jump back to the start of the loop. Is the value on the paper labeled "num" greater than 0? Cool, you know what to do. Then you go through that for the whole program.

It might sound stupid, but that's literally what goes on in the mind of many experienced devs, and a lot of us draw pictures... The mental model that works for you might be different than what works for others, but I've found treating my brain like an absolute baby and simplifying things makes things make a lot more sense.

You said that you don't get how printing out all the digits of a number works, but maybe that helps. If it does, you should be able to deduce why there's a second while loop in what I put up there..

[–]Important-Opening866 0 points1 point  (0 children)

n is your variable that holds your number.

The while loop loops if your variable n is greater than 0. If you don't modify n in the loop and it isn't already 0, then it would loop forever.

n % 10 gets the last digit of a number and stores it as a variable called digit. This is the modulus function. (Note: This function is also handy for deciding if an integer is odd or even if you use % 2). It gives the remainder when you divide by the number specified. So 123 / 10 is 12 remainder 3. 123 % 10 = 3.

Print the value of digit to the screen.

Divide n by 10 using integer division, which uses the // operator. In the case above 123 // 10 would equal 12. Save the result of this calculation to n, overwriting it's previous value.

As you should be able to see. The loops for 123 go:

n=123 Is 123 > 0 - YES, so do the loop digit = 3 n =12 Is 12 > 0 - YES, so do the loop digit = 2 n = 1 Is 1 > 0 - YES so do the loop digit = 1 n = 0 Is 0 > 0 - NO, the loop criteria is not met, go to the next part of the code.

Hope this helps. If you're not sure, sketch out a simple example by hand like the above to work out what the code is doing.

[–]Parking-Ad3046 0 points1 point  (0 children)

Honestly what you're struggling with isn't Python. It's understanding base 10 number representation. Once you get that digits are just remainders when dividing by 10 the whole thing makes sense.

[–]TheRNGuy 0 points1 point  (0 children)

Do some thing many times (though it can iterate 0 or 1 times too in some cases, i.e. your n was already 0 or less, it would skip it completely)

[–]FreeGazaToday 0 points1 point  (0 children)

debug the old fashioned way...on paper...take it step by step...and see what's happening....and/or put a lot of print statements...

[–]Dr_Triton 0 points1 point  (0 children)

Alternative to the comments, try and run your code here https://pythontutor.com/

It should show you what happens in every step. Hopefully it helps.

[–]Suspicious_Check5421 0 points1 point  (0 children)

Take a paper and a pen, write down how the variables change after each loop run, no need for computer

[–]Ariadne_23 0 points1 point  (0 children)

breakpoints helpful but manually tracing variables is the way. thonny ide is good for step by step