这就是我到目前为止,它可能完全是垃圾.我想要做的是验证caminput1,以便格式为HH:MM:SS.
哈希是从我测试时开始的.
def cameraspeedcheck():
timeformat = ("%H:%M:%S")
caminput1 = input("At what time did sensor 1 actuate? ")
# is caminput1 = time(HH:MM:SS)
# time.strptime(caminput1[%H:%M:%S])
caminput1.strptime(timeformat)
# else cameraspeedcheck()
我对所有这些东西的语法,或者一般的编码都不是很有经验,但在你告诉我去查阅它之前.
我一直在四处寻找,我找不到任何解释整个过程的东西.
最佳答案
strptime
是datetime.datetime的类方法,它接受要解析为第一个参数的字符串,并将格式作为第二个参数.所以你应该这样做 –
def cameraspeedcheck():
timeformat = "%H:%M:%S"
caminput1 = input("At what time did sensor 1 actuate? ")
try:
validtime = datetime.datetime.strptime(caminput1, timeformat)
#Do your logic with validtime, which is a valid format
except ValueError:
#Do your logic for invalid format (maybe print some message?).