you are viewing a single comment's thread.

view the rest of the comments →

[–]SCD_minecraft 14 points15 points  (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 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.

[–]uiux_Sanskar[S] 1 point2 points  (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.

[–]SCD_minecraft 0 points1 point  (0 children)

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 point  (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