Beginner Python student here.
I was given the assignment to do:
I am very confused with these instructions of what exactly I am supposed to do.
How do I incorporate an if/elif ladder into this function? What exactly am I supposed to be calculating here? I don't understand this one bit about how my function should be able to enter different values and unit.
Would really appreciate if someone could help me out and get me started on this.
Thank you all for your time.
答案 0 :(得分:0)
Basically the question is expecting you to write custom function that accepting 2 parameters, first parameter getting value, second parameter specify unit. In your custom function, convert the first parameter value based on the second parameter unit into seconds, then trigger time.sleep
答案 1 :(得分:0)
For part 1 of your assignment, you need to write a function sleep(amt,unit)
that will use the time.sleep(amt)
function.
Basically if you want to sleep for 1 minute, your function should convert to 60s and call time.sleep(60)
To do this set up an if elif else chain based on all units of time that are necessary
def sleep(amt, unit):
#remember to import the time module here
if(unit == "days"):
amt *= 86400 #days converted to seconds
elif(unit == "ns"): #nanoseconds
amt /= 10**9 #nanoseconds converted to seconds
else:
raise ValueError("Not a unit")
time.sleep(amt)
Part 2 clarifies the units that are necessary are ms, s, minutes, and hours. That just means you only need expect that in your if else ladder. The unit conversion is simple math. Just convert the amount of the given unit into seconds in the if/else ladder and call the time.sleep
function on that amt at the end. Note: the units I used are not necessary, I included them as to not do your assignment for you while giving an example.