0
Operator Precedence
Hey I was learning about Operator Precedence and I just want to understand this code because according to my thinking (and If else rules ) the result should be ("Can't deliver lunch") but here it's printing ("Lunch being delivered") because of Operator Precedence but I want to understand how is it working here ( a little explanation about it)....... Thank you in Advance meal = "fruit" money = 0 if meal == "fruit" or meal == "sandwich" and money >= 2: print("Lunch being delivered") else: print("Can't deliver lunch")
2 Antworten
+ 1
"and" has higher precedence than "or" operator ,
https://docs.python.org/3/reference/expressions.html
0
as 'and' has higher precedence than 'or', the condition could be written:
meal=='fruit' or (meal=='sandwich' and money>=2)
you could then see that meal=='fruit' so condition is True...