asked 105k views
2 votes
Give certain time amount in seconds. Convert it to mm:ss format where mm stands for minute with two digits, and ss stands for second with two digits. For example, t=65

Output:
Time: 01:05
Another separate run: t=75
Output:
Time: 01:15
Please use condition to create this program. No other Python's library or built-in functions allowed. The code should work with different times.

1 Answer

6 votes

Final answer:

A Python program that converts seconds to mm:ss format using division for minutes and the modulus operator for seconds. It formats the output to ensure that both minutes and seconds are displayed with two digits.

Step-by-step explanation:

Converting a certain time amount in seconds to minutes and seconds (mm:ss format) can be accomplished using basic arithmetic operations. By dividing the total seconds by 60, you will obtain the minutes, and the remainder will be the seconds. Below is a simple Python program that will handle this conversion:

t = int(input('Enter the time in seconds: '))
minutes = t // 60
seconds = t % 60
if minutes < 10:
str_minutes = '0' + str(minutes)
else:
str_minutes = str(minutes)
if seconds < 10:
str_seconds = '0' + str(seconds)
else:
str_seconds = str(seconds)
print('Time:', str_minutes + ':' + str_seconds)

This program uses a conditional statement to format the minutes and seconds so that they are always displayed with two digits, as the question specifies.

answered
User CMPE
by
7.9k points

No related questions found