How to generate break condition of a repeat loop in Python during its execution?

Asked

Viewed 925 times

0

I’m trying to make a program in Python that during the execution of a loop, some command is waiting for an input condition while the loop is executed.

For example, a program that prints numbers in ascending order and 1 in 1 second from zero while waiting for the user to type a string "terminates". When such a string is read, the loop is stopped and stops printing the numbers on the screen.

I tried to do it like this, but it didn’t work:

#-*- coding: utf-8 -*-

import time

i = 0
mensagem = 0
while mensagem!='terminar':
    mensagem = str(raw_input())
    if mensagem=='terminar':
        print "encerrado"
    else:
        print i
        time.sleep(1)
        i+=1
  • 1

    Reading and writing on the console at the same time is hardly a good idea. Better you create a graphical interface as suggested by nosklo.

2 answers

2

Your problem is that the function raw_input() "locks" (Blocks) - that is - it waits for user input and does not return until that input comes. The program stops there! time.sleep() also only returns when time passes.

To get around this, one of the ways is to check before calling raw_input() if there is data to be read. In python we can use the module select. He waits, for a certain time in seconds, to see if he has data available to read. If you have data, it returns immediately, otherwise it only returns when you pass the specified time.

import sys import select

i = 0

while True:
    print i
    pode_ler = select.select([sys.stdin,],[],[], 1)[0]
    if pode_ler and raw_input().strip().lower() == 'terminar':
        break
    i+=1

Note that this only works on linux. On windows, select only works with sockets, so a simple solution like this is not possible - you would need to use a thread forwarding data to a "selectable socket".

But serious programs that read data from stdin while doing something else in windows are rare, so this is not very important. If you need to read data while displaying something else, I suggest you start making a graphical interface for your program, using a visual programming library like Tkinter - in this case your program will respond to events like the click of a button to finish.

0

Your code is almost up, only one instruction left break, in the block of if, simple like this, you would already "break" the repeat loop when the condition was reached.

And the block of while would be more logical if the value were in True by default, ensuring repetition.

    #-*- coding: utf-8 -*-

import time

i = 0
mensagem = 0
while True:
    mensagem = str(raw_input())
    if mensagem=='terminar':
        print "encerrado"
        break
    else:
        print i
        time.sleep(1)
        i+=1

Some sources:

What is the difference between break, pass and continue in Python?

https://docs.python.org/2.0/ref/break.html

Browser other questions tagged

You are not signed in. Login or sign up in order to post.