[英]How to make a program end using user input?
while True:
entrance = input()
if entrance != 'stop':
self.driver.refresh()
I am having a hard time understanding how to end a program only when the user types in something like 'stop'.我很难理解如何仅在用户键入“停止”之类的内容时结束程序。 Right now the program takes a pause and waits for user input before continuing.
现在程序会暂停并等待用户输入,然后再继续。 I want the program to keep running the if statement continuously and only stop when the user types in 'stop'.
我希望程序继续运行 if 语句,并且仅在用户输入“停止”时停止。 ( in other words, the program is listening to user input and if user doesn't type anything it keeps going, but only when the word 'stop' is typed does it come to an end.)
(换句话说,程序正在监听用户输入,如果用户没有输入任何内容,它会继续运行,但只有在输入“停止”这个词时它才会结束。)
This should work:这应该有效:
while True:
entrance = input()
if entrance != 'stop':
self.driver.refresh()
else:
exit()
Try This:尝试这个:
import sys
while True:
entrance = input()
if entrance == "stop":
sys.exit()
else:
self.driver.refresh()
You can have a thread doing something, eg, self.driver.refresh()
, in a loop and a flag that controls whether it should continue running, and the main thread waiting and checking for user input.您可以让一个线程在循环中执行某些操作,例如
self.driver.refresh()
和一个控制它是否应该继续运行的标志,以及等待并检查用户输入的主线程。 If the user enters a specific input, say, 'stop', you can update the flag to have the loop in the first thread stop.如果用户输入特定输入,例如“停止”,您可以更新标志以使第一个线程中的循环停止。
from threading import Thread
stop = None
def f():
while stop is None:
# do something, likely self.driver.refresh()
t = Thread(target=f)
t.start()
while True:
entrance = input()
if entrance == 'stop':
stop = True
break
t.join()
print('Thread killed')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.