Why does this code crash python?

Scott David Daniels scott.daniels at acm.org
Sun Nov 12 20:14:51 EST 2006


Mythmon at gmail.com wrote:
>...

>>>             if float(seconds[0]) < 10: seconds[0] = '0' +
>>> seconds[0][:-1]
>>>             if float(seconds[1]) < 10: seconds[1] = '0' +
>>> seconds[1][:-1]        I've not handled leading 0s on the seconds field (I tried, but can't
>> figure out how to specify zero-filled field width for floats -- works
>> for integers)
> 
> I ran into the same problem, thats why I did it the way I did, instead
> of continuing to wrestle with python's string formatting.

For this portion, you could quite simply:

      totalsecs = int(whatever)
      print '%02d:%02d' % divmod(totalsecs, 60)

Or, depending on your leading zero requirements:

      totalsecs = int(whatever)
      print '%2d:%02d' % divmod(totalsecs, 60)

--Scott David Daniels
scott.daniels at acm.org



More information about the Python-list mailing list