擅长:python、mysql、java
<p>当前程序必须等待input()完成。直到用户按enter键,编译器才移过第二行。因此,一旦输入完成,函数就会启动。你知道吗</p>
<p>这个计时器可以通过多种方式完成,例如线程或计时器。有一些这样的例子<a href="https://stackoverflow.com/questions/2846653/how-to-use-threading-in-python">here</a>和<a href="https://stackoverflow.com/questions/2866380/how-can-i-time-a-code-segment-for-testing-performance-with-pythons-timeit/2866456#2866456">here</a>。对于线程,您需要一个用于用户输入的进程和一个用于计时器的进程。但更好更简单的方法是使用计时器并存储当前时间。我相信下面的代码与您想要的有些相似:</p>
<pre><code>import time
start_time = time.time()
def get_time():
current_time = time.time()
total = current_time - start_time
return total
while True:
CommandInput = input()
if CommandInput == "uptime":
y = get_time()
print(str(round(y)) + " seconds")
</code></pre>
<p>变量start\u time记录开始时的当前时间。时间。时间()获取计算机上的当前时间(以秒为单位)。get_time函数在输入后调用,通过减去当前时间减去开始时间来查看粘贴了多少时间。这样就不需要多个循环。你知道吗</p>
<p>while True循环只是等待用户输入,然后打印时间。然后重复此操作,直到程序退出。你知道吗</p>