A little more: decimal_portion

Seymore4Head Seymore4Head at Hotmail.invalid
Sat Oct 4 11:16:50 EDT 2014


On Sun, 5 Oct 2014 01:07:39 +1000, Chris Angelico <rosuav at gmail.com>
wrote:

>On Sun, Oct 5, 2014 at 12:58 AM, Seymore4Head
><Seymore4Head at hotmail.invalid> wrote:
>>  A little more: decimal_portion
>>
>> Write a function that takes two number parameters and returns a float
>> that is the decimal portion of the result of dividing the first
>> parameter by the second. (For example, if the parameters are 5 and 2,
>> the result of 5/2 is 2.5, so the return value would be 0.5)
>>
>> http://imgur.com/a0Csi43
>>
>> def decimal_portion(a,b):
>>     return float((b/a)-((b//a)))
>>
>> print (decimal_portion(5,2))
>>
>> I get 0.4 and the answer is supposed to be 0.5.
>
>Work out exactly what your program is doing, step by step. Print out
>the intermediate steps in the calculation, and compare what the
>program's doing to what you expect to be happening. What's (b/a)?
>What's (b//a)? What's (b/a)-((b//a))?
>
>ChrisA
I did.  I included a screenshot of me doing just that.
The formula seems to work in the shell, but does not work as a
function, or I am missing something subtle again.



More information about the Python-list mailing list