Python Programming Exercises, Gently Explained
36
while totalSeconds ____ 3600:
hours += ____
totalSeconds -= ____
# Set minutes to 0, then add a minute for every 60 seconds removed from
# totalSeconds until totalSeconds is less than 60:
minutes = 0
while totalSeconds >= ____:
minutes += 1
totalSeconds -= ____
# Set seconds to the remaining totalSeconds value:
seconds = ____
# 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 > ____:
____.append(str(____) + 'h')
# If there are one or more minutes, add the amount with an 'm' suffix:
if minutes > 0:
hms.append(____(minutes) + 'm')
# If there are one or more seconds, add the amount with an 's' suffix:
if seconds > 0:
hms.append(str(seconds) + ____)
# Join the hour/minute/second strings with a space in between them:
return ' '.join(____)
The complete solution for this exercise is given in Appendix A and
https://invpy.com/hoursminutesseconds.py. You can view each step of this program as it runs under a
debugger at
https://invpy.com/hoursminutesseconds-debug/.
Dostları ilə paylaş: