我正在尝试使用currentSecond,minute和hour作为if语句的计数器但是当你打印出currentMinute时的结果有时是负数,当我将所有浮点数更改为int时我没有得到错误,它运行正常,我的算法有问题,或者是我想要完成的问题?
#assume some rate, for example
rate=2.2
startTime= float(time.time())
currentTime = (float(time.time())-startTime) * float(rate)
currentHour = float(currentTime/3600)
currentMinute = (float(currentTime-currentHour*3600)/60)
currentSecond = float(currentTime-currentHour*3600-currentMinute*60)
答案 0 :(得分:0)
如果你在进行数学运算之后只为currentTime行转换回int,那么结果将接近你想要的结果,但并不完美,这就是我所做的。
#this will be a float
rate=2.2
#this is a float like hirsh said
startTime= (time.time())
currentTime = int(time.time()-startTime) * float(rate))
currentHour = (currentTime/3600)
currentMinute = (currentTime-currentHour*3600)/60
currentSecond = (currentTime-currentHour*3600-currentMinute*60)