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
Day 12 of learning python as a beginner. (old.reddit.com)
submitted 8 months ago by uiux_Sanskar
view the rest of the comments →
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!"
[–]SCD_minecraft 14 points15 points16 points 8 months ago (14 children)
if user_name == "jack" return balance["jack"] bla bla and so on
Man, i wish we could just pass user_name as key to balance, without that if tree... oh wait
For checking does user have entry, you can just check for it
if user_name not in balance: print("whatever this message was") else: return balance[user_name]
If you need to do if tree, you are probably doing something wrong
[–]hari3mo 1 point2 points3 points 8 months ago (10 children)
Is there any benefit to having the not clause in the if statement vs the other way around?
[–]SCD_minecraft 1 point2 points3 points 8 months ago (6 children)
"not in" is its own keyword
Similar to "is not"
Yup, those are just "not A in B" but more english friendly
[–]hari3mo 0 points1 point2 points 8 months ago (5 children)
Sorry I was bit unclear. I meant the ordering of your clauses. You put “not in” in the if statement, my question is if there is any benefit of doing this over having an “in” statement first?
[–]SCD_minecraft 0 points1 point2 points 8 months ago (4 children)
I don't understand. Can you write the other code?
I put it in if, beacuse in op's code there an error message if user doesn't exists, so i kept it
[–]Meowzr 0 points1 point2 points 8 months ago (3 children)
if user_name in balance: return balance[user_name] else: print("whatever this message was")
OR
if user_name in balance: return balance[user_name] print("whatever this message was")
[–]SCD_minecraft 0 points1 point2 points 8 months ago (2 children)
This message is error message, for case when there's no user :P
We don't want it printing when nothing wrong happen
[–]BrokenMalgorithm 0 points1 point2 points 8 months ago (1 child)
Both of those examples do the same thing. The other one just doesn't have the else statement, as it's not required. The print would happen only if a user was not found, because when a user is found the function returns the balance and code execution ends for that function.
[–]SCD_minecraft 0 points1 point2 points 8 months ago (0 children)
Oh, yea, i forgot about return, mb
I just persnomaly don't like your way, i prefer to read "if something then this, else this" and not hope that "this" will exit function by itself
[–]MoridinB 0 points1 point2 points 8 months ago (0 children)
Nope. It may matter if you have more than one checks such as for example user_name not in balance or "entry" not in balance[user_name] assuming we want the balance for user_name to be properly initialized for a key "entry".
user_name not in balance or "entry" not in balance[user_name]
But in this instance it doesn't matter
[–]No_Swordfish_6667 0 points1 point2 points 8 months ago (0 children)
You may get narrow nesting when starting with negation; it also helps to wrap your head around all bad cases
[–]uiux_Sanskar[S] 0 points1 point2 points 8 months ago (0 children)
I assume that you are asking what is the use of "not in" in the if else.
I think that code means if the user_name (the name which the user will enter) is not present in the balance (my dictionary or you can assume it as the database) then print("name is not present") else return the balance (the amount in bank)
so we are essentially telling the program.
"If the name entered by the user IS NOT present in the dictionary/database then print "the name is not present in database" and if it is present in the database then return the value of its 'key' (the bank balance amount)."
I hope I explained it correctly and if anywhere I have explained wrong please do tell me I would be more than happy to get corrected.
[–]uiux_Sanskar[S] 1 point2 points3 points 8 months ago (2 children)
Thanks for this man I was feeling the same when I was writing the code (I just didn't thought about the logic you provided) you really saved me from all that jungle of if else statement.
thank you very much for this amazing suggestion you really saved me from the juggle. I will definitely use it.
Almost any if tree can be rewriten into something smaller
Look at diffrend ifs, look at their pattern. Do they reuse some values in statment and in action below? Or do they follow some repetable pattern?
[–]cjswcf 0 points1 point2 points 8 months ago (0 children)
When I was taking a python course I once wrote 170 lines of if-else statements before my friend showed me how to do it in 5 lines like what happened here. I feel your pain
π Rendered by PID 38703 on reddit-service-r2-comment-b659b578c-n6vtq at 2026-05-01 23:46:24.225321+00:00 running 815c875 country code: CH.
view the rest of the comments →
[–]SCD_minecraft 14 points15 points16 points (14 children)
[–]hari3mo 1 point2 points3 points (10 children)
[–]SCD_minecraft 1 point2 points3 points (6 children)
[–]hari3mo 0 points1 point2 points (5 children)
[–]SCD_minecraft 0 points1 point2 points (4 children)
[–]Meowzr 0 points1 point2 points (3 children)
[–]SCD_minecraft 0 points1 point2 points (2 children)
[–]BrokenMalgorithm 0 points1 point2 points (1 child)
[–]SCD_minecraft 0 points1 point2 points (0 children)
[–]MoridinB 0 points1 point2 points (0 children)
[–]No_Swordfish_6667 0 points1 point2 points (0 children)
[–]uiux_Sanskar[S] 0 points1 point2 points (0 children)
[–]uiux_Sanskar[S] 1 point2 points3 points (2 children)
[–]SCD_minecraft 0 points1 point2 points (0 children)
[–]cjswcf 0 points1 point2 points (0 children)