Debashish Saha wrote:

[...]
why did i get different values for the same input?

Please choose a sensible subject line when posting.

The problem is with the division. Watch:


py> 21/2
10
py> 21.0/2
10.5


By default, division in Python 2 is integer division: any remainder is ignored. To use floating point division, you need to make one of the arguments be a float.

In your function, you have division y/5. When y is 11, 11/5 gives 2 exactly. If y is 11.0, you get 2.2 (and a tiny bit, due to rounding).

This is confusing, so in Python 3 division works more like you expect, and you don't have to worry about this nonsense. Also, starting in Python 2.4, you can get the new, calculator-like behaviour by using a special command to change the interpreter:

py> 11/5
2
py> from __future__ import division
py> 11/5
2.2000000000000002


My advice is to always use "from __future__ import division" at the start of your programs.



--
Steven

_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Reply via email to