r/PythonLearning • • Aug 31 '25

Can Somebody Please Help Me Understand This?

Post image

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….

85 Upvotes

27 comments sorted by

19

u/FriendlyFoeHere Aug 31 '25

The first block prints the counter after the while loop exits, when you reach 2

4

u/Zealousideal_Key_149 Aug 31 '25

😱 I didn’t even notice that until now lol thank you

3

u/ernee_gaming Sep 03 '25

With programming you'll have to get used to the: "Oh I'm a fucking moron" feeling when you notice what you did wrong. I feel it every day :-D doesnt get better.

1

u/ReachOutAndTouchDees Sep 01 '25

Additionally python always has the beginning index of zero unless you change the count as you already started at one the the print function is out of the whole loop.

1

u/Upbeat-Fly9656 Sep 02 '25

there's no lists here?

4

u/Zealousideal_Key_149 Aug 31 '25

Yup…order makes all the difference. Thank you Reddit Community!

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

>>> print

second loop has

>>> while

... print

... 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

u/moleculadesigner Aug 31 '25

Try to unwrap the loop by hands and watch what’s happening

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

u/whodevx Aug 31 '25

My presentation code after 3 years in IT

1

u/NaiveEscape1 Aug 31 '25

indent the print statement in the first block

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

u/NickU252 Sep 01 '25

Do your own homework...

1

u/[deleted] 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

u/[deleted] 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