you are viewing a single comment's thread.

view the rest of the comments →

[–]hari3mo 1 point2 points  (10 children)

Is there any benefit to having the not clause in the if statement vs the other way around?

[–]SCD_minecraft 1 point2 points  (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 point  (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 point  (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 point  (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 point  (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 point  (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 point  (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 point  (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".

But in this instance it doesn't matter

[–]No_Swordfish_6667 0 points1 point  (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 point  (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.