Path: csiph.com!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail From: Peter Otten <__peter__@web.de> Newsgroups: comp.lang.python Subject: Re: raw_input and break Date: Thu, 05 Nov 2015 12:38:05 +0100 Organization: None Lines: 68 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Trace: news.uni-berlin.de EQ8A6i+vYS3OnxQS6ioo4w9FLr+nQ5AJUzJ38RES9new== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'finally:': 0.05; 'none:': 0.05; 'sys,': 0.07; 'ioerror:': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'def': 0.13; 'contextlib': 0.16; 'nonblocking': 0.16; 'read:': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'received:t-ipconnect.de': 0.16; 'stdin': 0.16; 'subject:break': 0.16; 'true:': 0.16; 'url:faq': 0.16; 'wrote:': 0.16; 'script.': 0.18; 'try:': 0.18; 'input': 0.18; 'import': 0.24; 'unix': 0.24; 'module': 0.25; 'script': 0.25; 'header:User-Agent:1': 0.26; 'header:X-Complaints-To:1': 0.26; 'yield': 0.27; 'cat': 0.29; 'code': 0.30; 'folder': 0.30; 'problem': 0.33; 'url:python': 0.33; 'choices': 0.33; 'skip:~ 10': 0.33; 'except': 0.34; 'url:org': 0.36; 'url:library': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'received:org': 0.37; 'no,': 0.38; 'to:addr:python.org': 0.40; 'received:de': 0.40; 'waiting': 0.60; 'your': 0.60; 'press': 0.61; 'further': 0.62; 'skip:n 10': 0.62; 'here': 0.66; 'below:': 0.71 X-Injected-Via-Gmane: http://gmane.org/ X-Gmane-NNTP-Posting-Host: p57bd897d.dip0.t-ipconnect.de User-Agent: KNode/4.13.3 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:98296 input/ldompeling@casema.nl wrote: > > choices = raw_input("letter s to stop:") > > Oh no, this is not what I want. Now it is waiting for input when its go > further with the script. Because I have an while True: so I want that the > script go's continue only when I press a key then it must stop the script. Solutions to that problem are OS-dependent. For Unix terminals https://docs.python.org/2/faq/library.html#how-do-i-get-a-single-keypress-at-a-time shows one way which I used to write the contextmanager below: $ cat capture_key.py import termios, fcntl, sys, os from contextlib import contextmanager @contextmanager def nonblocking(stdin=None): if stdin is None: stdin = sys.stdin fd = sys.stdin.fileno() oldterm = termios.tcgetattr(fd) newattr = termios.tcgetattr(fd) newattr[3] = newattr[3] & ~termios.ICANON & ~termios.ECHO termios.tcsetattr(fd, termios.TCSANOW, newattr) oldflags = fcntl.fcntl(fd, fcntl.F_GETFL) fcntl.fcntl(fd, fcntl.F_SETFL, oldflags | os.O_NONBLOCK) def read(n): try: return stdin.read(n) except IOError: return "" try: yield read finally: termios.tcsetattr(fd, termios.TCSAFLUSH, oldterm) fcntl.fcntl(fd, fcntl.F_SETFL, oldflags) Put the capture_key.py module into the same folder as your script and use it like in the demo below: from capture_key import nonblocking # put your code here with nonblocking() as read: print("letter s to stop:") while True: enable_servo() servo(90) mindist = 80 choices = read(1) if choices == 's': print("stop") break if mindist > us_dist(15): bwd() print("backward 1x")