我正在试着做一个闹钟。如何从用户输入创建datetime对象,其中用户输入分别是小时、分钟和秒。例如:
from datetime import datetime
# user-input: at 2:30
hr = "2"
m = "30"
s = "0"发布于 2021-05-14 14:29:19
from datetime import datetime
def GetDate():
isValid=False
while not isValid:
userIn = input("Type Date dd/mm/yy: ")
try: # strptime throws an exception if the input doesn't match the pattern
d = datetime.datetime.strptime(userIn, "%d/%m/%y")
isValid=True
except:
print("Invalid")
return d
#test the function
print(GetDate())https://stackoverflow.com/questions/67529883
复制相似问题