+ 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?
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 ;)
+ 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())
+ 1
Thank You guyz for Your Co-operation.
0
can u plz rewrite the code!?
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