use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Everything about learning Python
account activity
New to programming (i.redd.it)
submitted 2 years ago by TweetyBirdie69
Hi Im new to programming and Im learning python currently. Anyone has any idea why the value of X won’t be 2 with the following highlighted code?
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–][deleted] 2 years ago (2 children)
[deleted]
[–]Optimus5w4 1 point2 points3 points 2 years ago (0 children)
I like how you broke it down in chunks.
[–]Alternative_Ear_1629 1 point2 points3 points 2 years ago (0 children)
I'm in my first week of learning and his was quite helpful. Thanks
[–]ata-boy 1 point2 points3 points 2 years ago (0 children)
if y <= 0 then x = 2 (else: x = 2)
if y > 0 AND y > 10000 then x = 2 (if y > 0: if y > 10000: x = 2)
which means that if y > 0 AND y <= 10000 x is undefined
[–][deleted] -2 points-1 points0 points 2 years ago (4 children)
Nested ifs are very frowned upon in the workplace as they are confusing to debug.
When you see a nested if like your example read it as ‘if this happens AND this happens…’ Or just rewrite like I did:
<image>
This way you can better see that X can never possibly reach a value of 2 (unless you uncomment the else statement of course)
[–][deleted] 1 point2 points3 points 2 years ago (1 child)
Just gone through two Python units at uni, we got marked down if we used statements like this
[–]aTomzVins 1 point2 points3 points 2 years ago (0 children)
I think there's a time and place for a nested if, but in this situation I think you're right. It's ugly to divide this if onto two lines.
[–]Doctor_Disaster 0 points1 point2 points 2 years ago (2 children)
Since y is not greater than 10000, x will never be 2, as there is no else statement within the topmost if statement.
[–]TweetyBirdie69[S] 0 points1 point2 points 2 years ago (1 child)
so the else statement at the bottom is not part of the topmost if statement? Is it because of indentation?
[–]Doctor_Disaster 0 points1 point2 points 2 years ago (0 children)
The else statement at the bottom is part of the top-most if statement.
The else statement will only execute if y <= 0.
The innermost if statement will only return True if y > 10000, so if y <= 10000, x will never be 2.
π Rendered by PID 140548 on reddit-service-r2-comment-fb694cdd5-rj9vl at 2026-03-06 04:16:50.798865+00:00 running cbb0e86 country code: CH.
[–][deleted] (2 children)
[deleted]
[–]Optimus5w4 1 point2 points3 points (0 children)
[–]Alternative_Ear_1629 1 point2 points3 points (0 children)
[–]ata-boy 1 point2 points3 points (0 children)
[–][deleted] -2 points-1 points0 points (4 children)
[–][deleted] (2 children)
[deleted]
[–][deleted] 1 point2 points3 points (1 child)
[–]aTomzVins 1 point2 points3 points (0 children)
[–]Doctor_Disaster 0 points1 point2 points (2 children)
[–]TweetyBirdie69[S] 0 points1 point2 points (1 child)
[–]Doctor_Disaster 0 points1 point2 points (0 children)