+ 1

why is the output of this code is 1.0 ,whereas i'm getting an answer of 9?

a = 6 b = 3 a /= 2 * b print(a) correct ans: 1.0 My ans : 9

9th Feb 2020, 1:29 PM
sai
13 Réponses
+ 6
sai a /= 2 * b is the same as: a = a / (2 * b)
9th Feb 2020, 1:54 PM
Aymane Boukrouh
Aymane Boukrouh - avatar
+ 3
How did you get 9?
9th Feb 2020, 1:36 PM
Aymane Boukrouh
Aymane Boukrouh - avatar
+ 3
Oma Falk answer is 1 and only 1. a/=2*b implies 2*b calculated at first and then we perform division. a/=2*b => a=a/(2*b) and no other stuff is legal/valid
9th Feb 2020, 2:01 PM
Jishnu Mukherjee
Jishnu Mukherjee - avatar
+ 3
sai according to your code this will be the only correct answer The correct answer is 1 as a/= 2*b is same as a/(2*b) = 6/(2*3) = 6/(6) = 1 Hence Proved Use bodmas to calculate this. Always use brackets and correct syntax to code. Hope you had understood
11th Feb 2020, 9:11 AM
Tanmay Gupta
Tanmay Gupta - avatar
+ 3
Code to get 9 //write the code a+= b //print a with the new value print(a)
11th Feb 2020, 9:13 AM
Tanmay Gupta
Tanmay Gupta - avatar
+ 3
a/=2*b is equivalent to a=a/(2*b) Therefore, a=6/(2*3) =6/6 =1.0
17th Feb 2020, 12:04 AM
Indira
Indira - avatar
+ 2
Jishnu Mukherjee that are true words. .. well 1.0... I simply debugged the thoughts of sai to understand what made him think it was 9.
9th Feb 2020, 2:09 PM
Oma Falk
Oma Falk - avatar
+ 2
2*b must be in ()
10th Feb 2020, 8:55 PM
Maryam Choupani Shirzi
Maryam Choupani Shirzi - avatar
+ 1
it is the priority of operator. * is higher than /= and therefore ti. e performed first.
9th Feb 2020, 1:48 PM
Oma Falk
Oma Falk - avatar
+ 1
@oma faik but what about associativity as both the * and / have same precedence LTR associativity is used,that is what confusing me
9th Feb 2020, 1:52 PM
sai
+ 1
Jishnu Mukherjee 😱😱😱😱😉😉 ups thanks for debugging
9th Feb 2020, 1:54 PM
Oma Falk
Oma Falk - avatar
+ 1
Oma Falk I did jackshit. first we compute 2*b which is 6 and then divide by a whose value is 6. so 6/6=1
9th Feb 2020, 1:57 PM
Jishnu Mukherjee
Jishnu Mukherjee - avatar
+ 1
sai right but our operators are /= and * and now it is clear for me why you asked. you saw / and * and... Jishnu Mukherjee a/2 =3 3*3 = 9 lets write it different a= a /(2*b)
9th Feb 2020, 1:58 PM
Oma Falk
Oma Falk - avatar