首页 > 解决方案 > 如何将日期时间值与 int 进行比较

问题描述

对于家庭作业,我的任务是创建一个游戏,并选择做一个基于反应时间的“快速绘图”风格的游戏。我目前正在使用 datetime 来查找玩家的反应时间,但我似乎无法弄清楚如何将datetime值与 if 语句中的 int 进行比较以确定获胜者。

我已经尝试过使用时间函数和解析,但似乎无法让它工作。

def playGameL1():
    drawTime = random.randint(3,7)
    print("You and the outlaw have lined up back to back and begin")
    print("taking 10 paces away from eachother...")
    time.sleep(5)
    print("The bystander who is conducting the quickdraw battle yells:")
    print("'Ready!'")
    time.sleep(drawTime)
    print("'DRAW!'")
    start = datetime.datetime.now()
    draw = input()
    end = datetime.datetime.now()
    reactionTime = (end-start)
    print ("Your reaction time was: ", reactionTime)

    if reactionTime<3:
        print("BANG!")
        time.sleep(1)
        print("You drew first and won the draw!")
        time.sleep(2)
        print("The next outlaw approaches...")
        playGameL2()
    else:
        print("BANG!")
        time.sleep(1)
        print("The outlaw drew first and you lost!")
        time.sleep(2)
        gameLoss()

我希望它确定获胜者并继续前进到下一个级别或我的 gameLoss 输出,但我收到错误。

我相信程序在调用其他函数时遇到了麻烦,因为它目前无法将datetime值与 int 进行比较,但我不太确定。任何帮助,将不胜感激。

标签: python-3.xdatetime

解决方案


reactionTime = (end-start).total_seconds()

将为您提供两个日期时间之间的秒数作为float.


推荐阅读