我正在编写一个程序,以在两个时间范围(小时,分钟)之间打印一定范围的时隙(小时,分钟对)。运行此程序时,我看到意外的输出:
import datetime,time
def stamp(hour, min):
return int(datetime.datetime(2018, 7, 17, hour,min).timestamp())
def ListSlotsAvailable(start, ent, durn):
secdurn=durn*60
for sec in range(start, ent, secdurn):
enttime = sec+secdurn
print("Stamp is %d-%d" % (sec, enttime))
print(hour_from_stamp(sec))
print(min_from_stamp(sec))
print(hour_from_stamp(enttime))
print(min_from_stamp(enttime))
def hour_from_stamp(stamp):
import datetime
print(datetime.datetime.fromtimestamp(stamp).strftime('%H'))
def min_from_stamp(stamp):
import datetime
print(datetime.datetime.fromtimestamp(stamp).strftime('%M'))
starthr=input("Give start time in hour")
startmin=input("Give start time in min")
start=stamp(int(starthr), int(startmin))
endhr=input("Give end time in hour")
endmin=input("Give end time in min")
ent=stamp(int(endhr), int(endmin))
durn=int(input("Give duration for each consultation"))
ListSlotsAvailable(start, ent, durn)
输出:
Give start time in hour10
Give start time in min15
Give end time in hour12
Give end time in min30
Give duration for each consultation30
Stamp is 1531802700-1531804500
10
None
15
None
10
None
45
None
Stamp is 1531804500-1531806300
10
None
45
None
11
None
15
None
Stamp is 1531806300-1531808100
11
None
15
None
11
None
45
None
Stamp is 1531808100-1531809900
11
None
45
None
12
None
15
None
Stamp is 1531809900-1531811700
12
None
15
None
12
None
45
None
1
我没想到在那里看到None值。他们来自哪里?
答案 0 :(得分:1)
您的函数应该返回这些字符串值,而不是打印:
def hour_from_stamp(stamp):
import datetime
return datetime.datetime.fromtimestamp(stamp).strftime('%H')
def min_from_stamp(stamp):
import datetime
print datetime.datetime.fromtimestamp(stamp).strftime('%M')
否则,没有显式返回值的函数将默认返回None
。