<div dir="ltr">Il giorno 16 giugno 2013 14:42, Gianni Di Noia ha scritto:<br><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
<div dir="ltr"><br><div class="gmail_extra"><div class="gmail_quote"><div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
<div>
<br>
> def percentage(part, total):<br>
> return part * 100.0 / total<br>
><br>
> >>> percentage(8, 80)<br>
> 10<br>
<br>
</div>Questo significa: "la percentuale di ottanta che dà come risultato otto è<br>
dieci". Poco chiaro e prono ad errori. Perché inverti i fattori?</blockquote><div><br></div></div><div>se ho preso 8 mele da una cesta che ne contiene 80, che % ho preso?</div><div>capita spesso di rispondere a domande così</div>
</div></div></div></blockquote></div><div dir="ltr"><br></div><div>Perfetto. Ma allora non si tratta di «<font face="courier new, monospace">8% of 80</font>», bensì del rapporto fra 8 e 80 (i.e. otto ottantesimi) espresso in centesimi (un tizio barbuto direbbe che «le parole sono importanti»). Del tipo:</div>
<div><br></div><div><span style="font-family:'courier new',monospace"> def </span><span style="font-family:'courier new',monospace">hundredths_from_ratio</span><span style="font-family:'courier new',monospace">(amount, denominator=None):</span><br>
</div><div><div style><span style="font-family:'courier new',monospace"> </span><span style="font-family:'courier new',monospace"> </span><font face="courier new, monospace">result = </font><span style="font-family:'courier new',monospace">amount</span><font face="courier new, monospace"> * 100.0</font><br>
</div></div><div style><font face="courier new, monospace"> if denominator:</font></div><div><font face="courier new, monospace"> </font><font face="courier new, monospace"> result /= </font><span style="font-family:'courier new',monospace">denominator</span></div>
<div style><span style="font-family:'courier new',monospace"> return result</span><br></div><div style><span style="font-family:'courier new',monospace"><br></span></div><div><div style><span style="font-family:'courier new',monospace"> >>> hundredths_from_ratio(8, 80)</span><br>
</div></div><div style><span style="font-family:'courier new',monospace"> 10.0</span></div><div><div><span style="font-family:'courier new',monospace"> >>> otto_ottantesimi = 8 / 80</span><font face="courier new, monospace"><br>
</font></div></div><div><div><span style="font-family:'courier new',monospace"> >>> </span><span style="font-family:'courier new',monospace">hundredths_from_ratio</span><span style="font-family:'courier new',monospace">(otto_ottantesimi)</span><font face="courier new, monospace"><br>
</font></div></div><div><div><span style="font-family:'courier new',monospace"> 10.0</span></div></div><div><br></div><div style>Può avere senso?</div><div style><br></div><div style>--</div><div style>Nadir</div>
</div></div>