简体   繁体   English

如何使用用户输入使程序结束?

[英]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.

相关问题 如何使用用户输入结束程序? - How to use user input to end a program? 我正在尝试在我的 class 中创建一个用户输入字典以获得额外的信用,但我无法弄清楚如何正确结束程序 - I'm trying to make a user input dictionary for extra credit in my class and I can't figure out how to properly end the program 如何让程序要求用户输入长度并使用输入来生成密码,只要用户愿意 - how to make the program ask the user for a input for length and use the input to make the password as long as the user would like 我如何循环并在用户输入特定输入后结束程序, - How do I loop, and end the program after the user has put in a particular input, 如果输入==“退出”并使用许多if语句,如何结束程序? - How to end program if input == “quit” with many if statements? 如何在我的程序中创建一个循环以使其在 python 中每个用户选项的末尾运行? - How can I create a loop in my program to make it run at the end of each user option in python? 如何使程序在python中的空白行处结束? - How to make a program end on a blank line in python? 如何使程序在单词末尾分开单词? - How to make a program separate words by the end of the word? 如果用户输入为空,如何在类中结束方法 - How to end a method in a class if user input is blank 如何在键盘上获取用户输入作为按键,进行检测,并使程序相应地在Python中起作用? - How to get user input as a key press, detect it, and make program act accordingly in Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM