[Tutor] What does "TypeError: 'int' object is not iterable" mean?

Richard D. Moores rdmoores at gmail.com
Fri Oct 22 15:42:50 CEST 2010


On Thu, Oct 21, 2010 at 15:44, Richard D. Moores <rdmoores at gmail.com> wrote:
>> If you want to control the number of decimal places in the string
>> formatting do something like:
>>
>>>>> i = 3
>>>>> print ("NEW LOW: %%.%sf at %%s" % i) % (lowz, timestamp)
>> NEW LOW: 81.750 at 22:55:13
>>>>> i = 6
>>>>> print ("NEW LOW: %%.%sf at %%s" % i) % (lowz, timestamp)
>> NEW LOW: 81.750000 at 22:55:13
>
> Thanks very much for the string formatting instruction.

So I wrote a function:

def float2n_decimals(floatt, n):
    """
    Given a float (floatt), return floatt to n decimal places.

    E.g., with n = 2, 81.34567 -> 81.35
    """
    return ("%%.%sf" % n) % floatt

which works fine, but a question remains: n is an integer. Why the 's'
in '%sf'?

(Also, please suggest a better name for this function.)

See this latest revision at <http://tutoree7.pastebin.com/K9ikqNFC>.

Dick


More information about the Tutor mailing list