0
[SOLVED] What is the order of the operators for this? Can't figure it out. Thank you.
4 Réponses
+ 16
The statement is:
print(int(1+2-3*4/5%6))
*, /, //, % are given higher precedence over + and -, and associativity is from left to right, which means that operations with same precedence operators are solved left to right.
Considering the part 3*4/5%6, since these operators have higher precedence, and solving it left to right,
3*4=12
12/5=2.4
2.4%6=2.4
3*4/5%6 = 12/5%6 = 2.4%6 = 2.4
Therefore 1+2-3*4/5%6 = 1+2-2.4 = 3-2.4 = 0.6
And since 0.6 is being converted to an integer, the decimal part is truncated and you're left with 0.
Hope this helped ☺
+ 2
1 + 2 - (((3*4) / 5) % 6)
Which equals 0.6, you were using an int which outputs 0 instead of a float.
+ 1
Thank you both for answers, my mistake was I didn't realize that 12/5 will return float 2.4 and not just int 2. O:-)
+ 1
thanks sheena...it was very usefull