P ython p rogramming e xercises



Yüklə 1,51 Mb.
Pdf görüntüsü
səhifə30/124
tarix14.05.2023
ölçüsü1,51 Mb.
#113537
1   ...   26   27   28   29   30   31   32   33   ...   124
PythonProgrammingExercisesGentlyExplained

Solution Template 
Try to first write a solution from scratch. But if you have difficulty, you can use the following 
partial program as a starting place. Copy the following code from https://invpy.com/hoursminutesseconds-
template.py and paste it into your code editor. Replace the underscores with code to make a working 
program: 
def getHoursMinutesSeconds(totalSeconds): 
# If totalSeconds is 0, just return '0s': 
if totalSeconds == ____: 
return ____ 
# Set hours to 0, then add an hour for every 3600 seconds removed from 
# totalSeconds until totalSeconds is less than 3600: 
hours = 0 


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/

Yüklə 1,51 Mb.

Dostları ilə paylaş:
1   ...   26   27   28   29   30   31   32   33   ...   124




Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©azkurs.org 2024
rəhbərliyinə müraciət

gir | qeydiyyatdan keç
    Ana səhifə


yükləyin