220k views
2 votes
(Fill in the Missing Code?) Replace the ***s in the seconds_since_midnight function so that it returns the number of seconds since midnight. The function should receive three integers representing the current time of day. Assume that the hour is a value from 0 (midnight) through 23 (11 PM) and that the minute and second are values from 0 to 59. Test your function with actual times. For example, if you call the function for 1:30:45 PM by passing 13, 30 and 45, the function should return 48645.

def seconds_since_midnight(***, ***, ***):
hour_in_seconds = ***
minute_in_seconds = ***
return hour_in_seconds + minute_in_seconds + seconds
The code is python

1 Answer

7 votes

Final answer:

The missing code should define the function with three parameters for hour, minute, and second. It should calculate the number of seconds since midnight by converting hours to seconds and adding the seconds from hours, minutes, and the given seconds.

Step-by-step explanation:

The student is asking for help to fill in the missing parts of a Python function designed to calculate the number of seconds since midnight, given the current time in hours, minutes, and seconds. The correct way to fill in the missing parts is by specifying the parameters and the calculations for converting hours and minutes into seconds.

Here's the completed function:

def seconds_since_midnight(hour, minute, second):
hour_in_seconds = hour * 3600
minute_in_seconds = minute * 60
return hour_in_seconds + minute_in_seconds + second

This function multiplies the number of hours by 3600 (the number of seconds in an hour), the number of minutes by 60 (the number of seconds in a minute), and adds these to the number of seconds to find the total seconds since midnight. For example, the time 1:30:45 PM, which is 13 hours, 30 minutes, and 45 seconds after midnight, would return 48645 seconds when this function is called as seconds_since_midnight(13, 30, 45).

User Oneman
by
7.8k points