使用时间库进行了秒表,它可以按预期工作,但也没有返回
我是Python的新手,所以我正在遵循一些教程,以了解如何做我喜欢做的事情。秒表不仅返回了时间,还返回了它开始和完成的时间。我的计划开始随时间创建两个变量。时间,一个用于开始,另一个用于结束。因此,我减去了最后的时间减去开始的开始,并取得了时间。然后,我又添加了两个变量,一个变量是在程序停止计数的时间,另一个是在启动时间开始的。在整理时间里,这很简单,只是使用dateTime.now(),在开始的时间开始,我认为TimeDELTA可能很有用,所以我确实减去了当前时间减去时间,然后开始时间开始。它正如我在标题中所说的那样的正常工作,除了一些细节,这也归还了我没有时间的时间,所以我不太确定为什么是Python的新手,并且从教程中了解到不要解释这些事情。如果您可以给我任何形式的解释,它将有很大帮助!
import time
from datetime import datetime, timedelta
time_ended=datetime.now()
def time_convert(sec):
mins = sec // 60
sec = sec % 60
hours = mins // 60
mins = mins % 60
print("Time Lapsed = {0}:{1}:{2}".format(int(hours),int(mins),sec))
input("Press Enter to start")
start_time = time.time()
input("Press Enter to stop")
end_time = time.time()
time_lapsed = end_time - start_time
time_started=timedelta(((time_lapsed/24)/60)/60)
print("Time started: ",time_ended-time_started)
print(time_convert(time_lapsed))
print("Time finished: ",time_ended)
这是运行程序后发生的事情
按Enter开始
按Enter停止
开始时间:2022-06-08 14:01:20.396229
时间lapsed = 0:0:53.04069781303406
无
时间完成:2022-06-08 14:02:13.436927
btw,我尝试删除,我尝试删除第一个“导入时间”,但它会促进我的代码,并试图做一些事情,例如将第二行的末端放在“,时间”再次删除第一行,它也不起作用。
I'm new to python, so I was following a few tutorials to understand how to do something I liked to do. A stopwatch returning not only the time passed but also the time it started and finished. My plan started creating two variables with time.time, one for the start and another for the finish. So I subtracted the final time minus the start, and get time lapsed. Then I added two more variables, one for the time the program stopped counting, and another for the time it started. For the finish time it was easy, just used dateTime.now(), and for the time start I decided timeDelta might be usefull, so I did subtracted current time minus time lapsed, and get time started. It worked as expected as I said in the title, except for a little detail, which is also returning a None value apart of my time lapsed, so I'm not quite sure why being new to Python and being all I know from tutorials that don't explained these things. If you could give me any sort of explanation it'll help a lot!
import time
from datetime import datetime, timedelta
time_ended=datetime.now()
def time_convert(sec):
mins = sec // 60
sec = sec % 60
hours = mins // 60
mins = mins % 60
print("Time Lapsed = {0}:{1}:{2}".format(int(hours),int(mins),sec))
input("Press Enter to start")
start_time = time.time()
input("Press Enter to stop")
end_time = time.time()
time_lapsed = end_time - start_time
time_started=timedelta(((time_lapsed/24)/60)/60)
print("Time started: ",time_ended-time_started)
print(time_convert(time_lapsed))
print("Time finished: ",time_ended)
This is what happen after running the program
Press Enter to start
Press Enter to stop
Time started: 2022-06-08 14:01:20.396229
Time Lapsed = 0:0:53.04069781303406
None
Time finished: 2022-06-08 14:02:13.436927
BTW, I did try to delete the first "import time" but it brokes my code, and trying to do something like putting at the end of the second line a ",time" deleting the first line again, it doesn't work neither.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
print(time_convert(time_lapsed))
是您的问题。
tine_convert方法没有返回任何内容。因此,通过将函数封装在打印功能中,它只是无打印。
尝试在方法的末尾返回字符串,例如:
返回“测试”
print(time_convert(time_lapsed))
Is your problem.
The tine_convert method doesn't return anything. So by encapsulating the function in a print function it just prints none.
Try returning a string in the end of your method like:
Return "test"