-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtask7.py
53 lines (42 loc) · 1.26 KB
/
task7.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import time
import sys
import datetime
from random import randint
from timeit import default_timer as timer
def sleep_random_time():
time.sleep(randint(0, 9))
def measure():
start = timer()
sleep_random_time()
end = timer()
file = open("measure.txt","w")
print("Slept for ", end - start)
file.write("Current date: ",)
file.write("{:%B %d, %Y}".format(datetime.datetime.now()))
file.write(" Slept for: ")
file.write(str(end-start))
file.close()
def sleep_and_repeat():
delay = int(input("Enter time to sleep!"))
if delay < 0:
print("Negative time! Bye-bye!")
exit(0)
else:
time.sleep(delay)
sleep_and_repeat()
def date_calculator():
bday = input("Enter your birthdate in format yyyy-mm-dd\n")
birth_date = datetime.datetime.strptime(bday,'%Y-%m-%d')
date = datetime.date.today()
delta = date-birth_date.date()
print("You are live ",delta.days," days\n")
if __name__ == "__main__":
if len(sys.argv) == 1 or sys.argv[1] == 'sleep':
sleep_and_repeat()
elif sys.argv[1] == 'measure':
measure()
elif sys.argv[1] == 'date':
date_calculator()
else:
print("Invalid argument! Expected 'sleep', 'measure' or 'date'")
exit(0)