Error with "not all arguments converted during string formatting"


#1

Hi!! Currently I’m working on converting milliseconds to days, hours, minutes, seconds and ms in IDLE Python, however I’m stuck on line 3 as the full module goes like this:

ms = (input("Input number of milliseconds: "))
sec = (ms * 1000)
minute = ms % 1000 * 60
hr = (((ms % 1000) % 60) * 60)
day = (((ms % 1000) % 60) * 24)
print(“Results = %f day(s), %f hour(s), %f minutes(s), %f second(s),
and %f millisec(s)” % (day, hr, minute, sec, ms))

I’m wondering what needs to fix, please reply before midnight(GMT+7).

Strings and Console Output


#2

Your code is more readable when parens are kept to a minimum…

ms = input("Input number of milliseconds: ")
sec = ms * 1000
minute = ms % 1000 * 60
hr = ms % 1000 % 60 * 60
day = ms % 1000 % 60 * 24
print(“Results = %f day(s), %f hour(s), %f minutes(s), %f second(s),
and %f millisec(s)” % (day, hr, minute, sec, ms))

Are you sure you want to multiply?

>>> ms = 212212
>>> sec = ms // 1000
>>> sec
212
>>> 

#3

No, however, I tried out some more formats and get "unsupported operand type(s) for /: ‘int’ and ‘str’ " in this line of module (days = float(ms / 86400000))


#4

days should be an integer, not a float. Note that in Python 3 all quotients are floats unless declared as integers or we use integer (floor) division.

>>> milliseconds = 7 * 24 * 60 * 60 * 1000 // 2
>>> days = milliseconds // 24 // 3600 // 1000
>>> days
3
>>> rem = milliseconds - days * 24 * 3600 * 1000
>>> hours = rem // 3600 // 1000
>>> hours
12
>>> 

So far the math holds up, given we started with 3 and a half days.


#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.