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")

19th Feb 2021, 9:02 AM
Balraj Singh
2 Antworten
+ 1
"and" has higher precedence than "or" operator , https://docs.python.org/3/reference/expressions.html
19th Feb 2021, 9:10 AM
Abhay
Abhay - avatar
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...
19th Feb 2021, 12:28 PM
visph
visph - avatar