r/PythonLearning • u/Zealousideal_Key_149 • Aug 31 '25
Can Somebody Please Help Me Understand This?
So I understand the second code block: once the number reaches 3, the print condition no longer applies. But with that same logic, why is the first block 2 and not 1? I didn’t know how to begin a google search for this one….
4
u/Zealousideal_Key_149 Aug 31 '25
1
u/ninhaomah Aug 31 '25
no , its not the order. its the difference in the instruction to the machine which can be seen right off the screen visually. why do you say its the same logic ?
the first loop has
>>> while
... counter
second loop has
>>> while
... counter
the print lines begin with two different symbols , ... vs >>> .. clearly they are not the same. how ? or why ? thats the coding / programming knowledge and skill but they are NOT the same visually. just 1 look and two prints are different.
1
u/Klikis Aug 31 '25
It is not the order, but the indentation
In first one the it goes 0-print-1-print-2
In the second one 0-1-2-print
Swapping order in first one would give you: 0-1-print-2-print and you'd get 1 2 As an output
2
2
u/Sea-Ad7805 Aug 31 '25
Run your code step by step in a debugger to understand what is going on: https://memory-graph.com/#code=%0Acounter+%3D+1%0Awhile+counter+%3C+2%3A%0A++++counter+%2B%3D+1%0A++++%0Aprint%28counter%29%0A%0Acounter+%3D+0%0Awhile+counter+%3C+3%3A%0A++++print%28counter%29%0A++++counter+%2B%3D+1%0A%0A&play
2
u/waroftheworlds2008 Aug 31 '25
If the white space is confusing you, i reccomend going to C or C++.
1
u/YetAnohterOne11 Aug 31 '25
Then the opposite problem will surface: Why is this line outside of the if / for / while block, it visually looks like it should be a part of that block?
Plus, C and C++, with their numerous gotchas (that tend to be hard to debug), are probably not the best languages for beginners.
1
u/waroftheworlds2008 Aug 31 '25
I learn better with hard and unambiguous rules. Python is too much like learning english, for me.
But for a first language, it really does depend on what you learn better with.
2
u/Throw2590Away Sep 01 '25 edited Sep 01 '25
Noobs asking for advice and genuinely taking on the feedback they've been given. Not even going to ai.
The system is healing.
Also to OP: Try looking into using a debugger. If you can download vscode, you can install an extension that will let you look at what's happening with all your variables at every step of your programs operation. It's the best way to find these sorts of mistakes yourself and the best way to learn in a way that will stick with you :)
If you get stuck trying to install it just search "debugging python in VS code on YouTube" and you'll get loads of playlists that will help.
Happy learning!
2
u/YUB-YUB Sep 05 '25
As for how to look up what code does when youve run into a block, just type “what does this code do” in google’s search window and paste your code in. Youll get back a nice description and helpful suggestions on how to do it better. Best of luck!
1
1
1
u/shaft196908 Aug 31 '25
I don't understand why using the debugger - step thru a program line by line and the answer becomes clear.
1
u/tankmissile Aug 31 '25
the first loop results in counter being printed after the increment. The second loop prints before the increment. If you put the print after the increment in the second loop it will instead say 1 2 3
1
1
Sep 02 '25
But with that same logic, why is the first block 2 and not 1?
The value of counter is 1 when the condition is checked for the last time (1 is less than 2); you then add 1 to it, and 1 + 1 is 2. Then the loop condition isn't true any more and the loop ends. That's when you print the value of counter, which is 2.
You can pretty easily resolve questions like this by not trying to do it in your head. Get a piece of paper and write down the name "counter" and note its value next to it. When the value changes, erase the value on your paper and update it. Go line by line and loop by loop; don't skip any steps.
1
u/SlightSurround5449 Sep 03 '25
I see you've already been helped I'm just proud that I knew the answer. Keep on going!
1
u/Minute_Journalist593 Sep 03 '25
first you have printed the statement outside the loop so its giving the last looped statement and in second one you have printed the statement inside the loop so it's get's printed every time the loop iterates
1
u/Arcca2924 Sep 04 '25
IMO this is why Python sucks. Indentation is super easy to get wrong and not even notice, especially for beginners. Proper code blocks with curly braces are way more obvious where they start and end.
0
Aug 31 '25
[deleted]
1
u/LanguageCommercial47 Aug 31 '25
I don't understand how its effecting the result
2
u/geo9797 Aug 31 '25
anything indented in the while loop is looping infinitely until the condition is met, but when the print is outside the loop (the loop is still happening but it loops inside until it meets its condition) THAN afterwards it moves to the unindented print


19
u/FriendlyFoeHere Aug 31 '25
The first block prints the counter after the while loop exits, when you reach 2