擅长:python、mysql、java
<p>您可以只使用一个类:</p>
<pre><code>import time
from threading import Thread
class stopwatch:
def __init__(self):
self.s = 0
self.m = 0
self.h = 0
self.stopped = False
self.name = "shirb"
def begin(self):
while self.stopped is False:
self.s += 1
if self.s >= 60:
self.s = 0
self.m += 1
if self.m >= 60:
self.m = 0
self.h += 1
time.sleep(1)
def get_time(self):
return str(self.h) + "h" + str(self.m) + "m" + str(self.s) + "s"
s = stopwatch()
Thread(target=s.begin).start()
input("press enter to stop the stopwatch")
s.stopped = True
print("Name: " + s.name + "\nTime: " + s.get_time())
</code></pre>
<p>这就解决了问题</p>