Do this Write this function in a file called sleep.p (haha it almost looks like
ID: 3816664 • Letter: D
Question
Do this Write this function in a file called sleep.p (haha it almost looks like sleepy) From the examples in the Summary section you can see what parameters this function will need to have. Inside this function, import the time module (yes, you can import a module inside a function). You need the time module because you will still need to use Python's standard time sleep function. Next, calculate how many seconds the duration should be based on the values of the parameters. For example: 1 s rightarrow 1 3 m rightarrow 3 * 60 = 180 50 ms rightarrow 50/1000 = 0.05 1.5 h 1 rightarrow 1.5 * 60 * 60 = 5400 Your function needs to handle only these units: 's' (seconds), 'm' (minutes) 'ms' (milliseconds, and 'h' (hours). If you want to add additional units, go ahead, but they won't be useful in this assignment. The last statement in the function should call the time. Sleep function to sleep for that many seconds. Test it Test the function with a few different values and units to be sure it works correctly. You can test the hours unit like this: Sleep Sleep(0.00028, 'h') That's about 1 second.Explanation / Answer
Hi Student,
Here is the sleep.py code.
def sleep(value,units ): #Function sleep definition is here
import time; # importing time module inside function
if (units =='h'): # handling 'h' units
value=value*60*60
time.sleep(value);
elif (units == 'm'): # handling 'm' units
value=value*60
time.sleep(value);
elif (units == 's'): # handling 's' units
time.sleep(value);
elif(units == 'ms'): # handling 'ms' units
value=value/1000;
time.sleep(value);
else:
print ("please enter only any of these units h,m,s and ms")
return;
Here is the test_sleep.py code. Place the both files in same folder.
import sleep # importing the sleep file which we have written earlier
sleep.sleep(0.06,'m'); # calling sleep fucntion
sleep.sleep(1,'s');
sleep.sleep(1000,'ms');
sleep.sleep(0.00028,'h');
sleep.sleep(5,'i');
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.