[Tutor] Floating point exercise 3 from Learn python the hard way

Válas Péter sulinet at postafiok.hu
Mon Jun 13 20:07:43 CEST 2011


2011/6/13 amt <0101amt at gmail.com>

> I came up with:
> print "Roosters", 100 - float(25) * 3 % 4
>
> This is for line 3 so it is more precised. Is it correct what I did?


I don't think so. All the operations in this line are for integers. % means
the remainder of the division,  so according to the precedence, the original
may be parenthesed as 100-(25*3%4), which is really 97 (75 divided by 4 is
18, and 3 remains).


> What do I have to do for line 5?
>

Hard to say without you share your Python version with us. Division behaves
differently in P2.x and P3.x

Also note that besides float() there is a more simple way to make a float
from an integer: write 1.0 instead of 1, and it becomes a float as well as
the result of the operation. Using the function is good if you work with a
variable.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110613/05efa93d/attachment.html>


More information about the Tutor mailing list