while True:
try:
TimeInput = input("What time is it now ? :")
ValidTime = ???????????????????????????????????
print (ValidTime)
break
except ValueError:
print ("Use Format Hours: Minutes (HH:MM)")
def get_input_time():
while True:
input = raw_input("What time is it now?\n")
try: # strptime throws an exception if the input doesn't match the pattern
input_time = datetime.datetime.strptime(input, "%H:%M")
break
except:
print("Use Format Hours:Minutes (HH:MM)")
return input_time
#test the function
input_time = get_input_time()
print("Time is %s:%s" % (input_time.hour, input_time.minute))
strptime函数可能会对您有所帮助。如果输入文本不是一个好的日期时间,它会抛出一个异常。 导入日期时间
相关问题 更多 >
编程相关推荐