python-3.x - 如何将日期时间值与 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 进行比较,但我不太确定。任何帮助,将不胜感激。
解决方案
reactionTime = (end-start).total_seconds()
将为您提供两个日期时间之间的秒数作为float
.
推荐阅读
- python - Python 表中聚合文件的计算字段
- javascript - 使用箭头动态添加的下拉导航
- c++ - 在 STL 中使用 operator== 进行命名空间解析
- python - 使用具有每小时时间序列作为输入的 LSTM 预测每日价值
- .net - 我需要使用哪种技术来构建可定制的(也是跨平台的)移动应用程序?
- matlab - 为什么我在 matlab 的“lla2eci”和“sgp4.propagate”之间有所不同?
- excel - 为什么我会收到以下 VBA 代码的 object required 错误?
- i2c - 如何在 NodeMCU 上设置 I2C?
- android - Xamarin.Android:解密文件随机损坏
- xslt - XSLT3 是否可以在与 expand-text=yes 相同的模板中使用禁用输出转义?