+ 2

Code working but not giving correct ans

hi all I had written a code : def func (x): return (x/150*100) func () in the above code when I write "func (98)" or any number in the place of x it is giving me answer "0" why?

17th Feb 2017, 2:07 PM
Knowledge Seeker
Knowledge Seeker - avatar
5 Réponses
+ 3
@Knowledge Seeker wrote: "no need to answer cuz I had finded out the problem in the above code we have to first multiple and then we have to dived" Yes, you're right... because of left to right priority between multiplication and division ( wich have same precedende level -- http://www.annedawson.net/Python_Precedence.htm ). So you can write: return x*100/150 But one clean other way to correct your code, is to add parenthesis to avoid implicite priorities getting explicite ones: return (x/150)*100 ... as your wrapping parenthesis are also unnecessary ;)
17th Feb 2017, 6:42 PM
visph
visph - avatar
+ 2
either replace 'return' by print or assign func() to a variable and print tat variable ...... ans=func() print (ans) ...... or or directly use print (func())
17th Feb 2017, 2:16 PM
rossi
rossi - avatar
+ 1
Thank You guyz for Your Co-operation.
18th Feb 2017, 6:22 PM
Knowledge Seeker
Knowledge Seeker - avatar
0
can u plz rewrite the code!?
17th Feb 2017, 3:03 PM
Knowledge Seeker
Knowledge Seeker - avatar
0
no need to answer cuz I had finded out the problem in the above code we have to first multiple and then we have to dived anyway thx alot
17th Feb 2017, 3:14 PM
Knowledge Seeker
Knowledge Seeker - avatar