Python Programming Exercises,
Gently Explained
138
# totalSeconds until totalSeconds is less than 60:
minutes = 0
while totalSeconds >= 60:
minutes += 1
totalSeconds -= 60
# Set seconds to the remaining totalSeconds value:
seconds = totalSeconds
# Create an hms list that contains the string hour/minute/second amounts:
hms = []
# If there are one or more hours, add the amount with an 'h' suffix:
if hours > 0:
hms.append(str(hours) + 'h')
# If there are one or more minutes, add the amount with an 'm' suffix:
if minutes > 0:
hms.append(str(minutes) + 'm')
# If there are one or more seconds, add the amount with an 's' suffix:
if seconds > 0:
hms.append(str(seconds) + 's')
# Join the hour/minute/second strings with a space in between them:
return ' '.join(hms)
Dostları ilə paylaş: