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

Alan Gauld alan.gauld at btinternet.com
Mon Jun 13 20:26:27 CEST 2011


"amt" <0101amt at gmail.com> wrote

1 print "I will now count my chickens:"
2 print "Hens", 25 + 30 /6
3 print "Roosters", 100 - 25 * 3 % 4 #Output is 97
4 print "Now I will count the eggs:"
5 print 3 + 2 + 1 - 5 + 4 % 2 - 1 / 4 + 6 #Output needs to be 6,83 but
Python give me 7
6 print "Is it true that 3 + 2 < 5 - 7? "
7 print 3 + 2 < 5 - 7
8 print "What is 3 + 2?", 3 + 2
9 print "What is 5 - 7?", 5-7
10 print "Oh, that's why it's False."
11 print "How about some more. "
12 print "Is it greater?", 5 > -2
13 print "Is it greater or equal?", 5 >= -2
14 print "Is it less or equal?", 5 <= -2

> I have to rewrite it to use floating point numbers so it's more 
> accurate. I
> have read the lines of code and only line 3 and 5 needs to be 
> changed.

Some things to consider:

Can you explain your reasoning? Why do you think line 3 needs
to be changed? Why line 5?

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

In what way do you think this is more precise?

I don't understand your comment on line 5.
Why would the answer be 6.83?
Can you add parentheses to the expression
showing how you arrive at 6.83?

HTH,

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/





More information about the Tutor mailing list