0
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'. ( 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.)

Bellfrank
  • 33
  • 5
  • maybe helpful: https://stackoverflow.com/a/53344690/10197418, an also https://stackoverflow.com/q/2408560/10197418 – FObersteiner Nov 20 '20 at 18:42
  • Does this answer your question? [Python nonblocking console input](https://stackoverflow.com/questions/2408560/python-nonblocking-console-input) – dwb Nov 20 '20 at 18:52

3 Answers3

0

This should work:

while True:
    entrance = input()
    if entrance != 'stop':
        self.driver.refresh()
    else:
        exit()
  • This isn't answering my question, this code will wait for user input, and if user types in stop, then yeah it will exit. I want the code to run and not wait for user input but only when the user types in stop does it stop. In other words, the user can not type anything and the if statement will execute. – Bellfrank Nov 20 '20 at 18:47
0

Try This:

import sys

while True:
  entrance = input()
  if entrance == "stop":
    sys.exit()
  else:
    self.driver.refresh()
The Pilot Dude
  • 2,091
  • 2
  • 6
  • 24
0

You can have a thread doing something, e.g., 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. 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')
Nikolaos Chatzis
  • 1,947
  • 2
  • 8
  • 17