【发布时间】:2015-08-26 15:02:48
【问题描述】:
我一直在搜索和搜索如何弄清楚如何使输入或某些内容进入 while 循环。如, input() 命令不会停止我的秒表。我尝试过 tkinter、pygame 和其他几种方法,但它们都不起作用。如果有人可以帮助我,如果可能的话,我更喜欢小而简单的东西。具体来说,我想学习做什么,基本上是允许,当按下任何键时,它会立即停止(最好不按回车键)。谢谢,马鞍猪!
这是我目前所拥有的,没有任何东西可以激活停止:
#Setup (Variables and stuff)
hours = 0
minutes = 0
seconds = 0
import time
#Main Part of Code
print("Welcome to PyWatch, a stopwatch coded in Python!")
print("Press any key to start the stopwatch.")
print("Then, press any key to stop it!")
start = input("")
while hours < 48:
seconds = seconds + 1
time.sleep(1)
print(hours, "hours,", minutes, "minutes,", seconds, "seconds")
#If Statements for getting seconds/minutes/hours
if (seconds == 60):
minutes = minutes + 1
seconds = seconds - 60
if (minutes == 60):
hours =hours + 1
minutes = minutes - 60
【问题讨论】:
-
尝试为同一个程序创建两个线程,一个运行秒表,另一个等待用户输入。