我想复制一下保罗·埃尔多斯小时候拉过的把戏: 根据他的出生日期和当前时间告诉某人他几秒钟。
这是当前代码的样子:
# choose your game mode
op = input("Choose gamemode - Two player = 2 One player = 1 : ")
if op == 2 :
print (two_player_op())
if op == 1:
print(one_player_op())
#One player Function
def one_player_op():
# I put game code for 1st play here
#Two player function
def two_player_op():
# I put game code for two player here
#code ends
结果大约是770xx秒,但这是不正确的,因为每天已经是36000 * 24秒。
那我该如何使用datetime库执行我想做的事情?
答案 0 :(得分:2)
您可以使用total_seconds计算两个日期之间的秒差
from datetime import datetime
year = int(input("year: "))
month = int(input("month: "))
day = int(input("day: "))
#Calculate time in seconds between now and the day of birth
time_in_seconds = (datetime.now() - datetime(year=year, month=month, day=day)).total_seconds()
print("You are {} seconds old.".format(time_in_seconds))
输出将为
year: 1991
month: 1
day: 31
You are 892979995.504128 seconds old.