試試
ans = random.randint(3,10)
print(ans)
if abs((time2-time1).seconds-ans) <= 0.5:
print("成功")
else:
print("失敗")
回頭想想
都是 int
好像沒什麼機會什麼 0.5 ...
datetime object 減 datetime object 會得出 datetime delta object
如果要比較大小,如 >, ==, >= 要兩個datetime delta object比較
if (time2-time1) >= datetime.timedelta(seconds=0.5):
print(“成功")
else:
print("失敗")
請搭配 Python datetime官方文件看
import datetime
import random
target = datetime.timedelta(seconds=random.randint(3, 10))
print(f'目標{target.seconds}秒')
input('請按任意按鍵開始')
start = datetime.datetime.now()
input('請按任意按鍵結束')
end = datetime.datetime.now()
print(f'經過{(end-start).total_seconds()}秒')
if abs((end-start) - target) <= datetime.timedelta(seconds=0.5):
print("成功")
else:
print("失敗")
input()