Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Dennis Lee Bieber Newsgroups: comp.lang.python Subject: Re: raw_input and break Date: Thu, 05 Nov 2015 20:09:38 -0500 Organization: IISS Elusive Unicorn Lines: 128 Message-ID: References: <8lM_x.24516$cf7.4240@fx02.am1> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Trace: news.uni-berlin.de hfteUH6g3w+VxBaQmoi7tg7nXO3orxm+BU6OSucc7TqA== 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; 'else:': 0.03; 'elif': 0.04; '(python': 0.05; '(so': 0.07; 'assignment': 0.07; 'enabled.': 0.07; '(1,': 0.09; '3),': 0.09; 'cmd': 0.09; 'message- id:@4ax.com': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'underlying': 0.09; 'output': 0.13; 'syntax': 0.13; 'def': 0.13; 'thu,': 0.15; '"invalid': 0.16; '"right"': 0.16; '"while"': 0.16; '1),': 0.16; '2),': 0.16; '80.': 0.16; 'command:': 0.16; 'forward:': 0.16; 'input.': 0.16; 'left,': 0.16; 'left:': 0.16; 'received:80.91.229.3': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'redundancy': 0.16; 'right:': 0.16; 'rotation': 0.16; 'sequence:': 0.16; 'skip:> 20': 0.16; 'skip:> 50': 0.16; 'subject:break': 0.16; 'tested)': 0.16; 'there?': 0.16; 'threading': 0.16; 'true:': 0.16; 'typo': 0.16; 'url:home': 0.18; 'input': 0.18; 'changes': 0.20; '2015': 0.20; '%s"': 0.22; '2.x': 0.22; 'arguments': 0.22; 'keyboard': 0.22; 'rid': 0.22; 'changes,': 0.23; 'import': 0.24; 'somewhere': 0.24; 'module': 0.25; "doesn't": 0.26; 'command': 0.26; 'header:X-Complaints-To:1': 0.26; 'raw': 0.27; 'defining': 0.27; 'sequence': 0.27; 'function': 0.28; 'key,': 0.29; 'code:': 0.29; 'print': 0.30; 'that.': 0.30; 'code': 0.30; "i'd": 0.31; 'lets': 0.33; 'open': 0.33; 'handle': 0.34; 'add': 0.34; 'could': 0.35; 'attempt': 0.35; 'false': 0.35; 'nov': 0.35; 'skip:> 10': 0.35; 'stopped': 0.35; 'something': 0.35; 'instead': 0.36; 'there': 0.36; '(and': 0.36; 'modules': 0.36; 'to:addr:python- list': 0.36; 'subject:: ': 0.37; 'really': 0.37; 'setting': 0.37; 'turn': 0.37; 'received:org': 0.37; 'charset:us-ascii': 0.37; 'things': 0.38; 'doing': 0.38; 'means': 0.39; 'does': 0.39; 'to:addr:python.org': 0.40; 'ever': 0.60; 'your': 0.60; 'press': 0.61; 'more': 0.63; '>from': 0.76; 'dennis': 0.91 X-Injected-Via-Gmane: http://gmane.org/ X-Gmane-NNTP-Posting-Host: adsl-108-68-178-61.dsl.klmzmi.sbcglobal.net X-Newsreader: Forte Agent 6.00/32.1186 X-No-Archive: YES 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:98324 On Thu, 05 Nov 2015 17:28:36 GMT, input/ldompeling@casema.nl declaimed the following: >Oke, lets try your code.Can you help me with that. >This is my code: >------------------------------------------------- >from gopigo import * >import time > >set_right_speed(150) >set_left_speed(105) Is there a typo there? If those are setting rotation speeds for independent drive wheels, you will have a slow left turn enabled. >enable_servo() >fwd() >print("forward 1x") >time.sleep(4) >stop() > >while True: > servo(90) > mindist = 80 > > if mindist > us_dist(15): "mindist" will always be 80 -- there is no code shown below that ever changes it (so the above assignment can be put before the "while" statement). That means your us_dist() function must somewhere return a value greater than 80. > bwd() > print ("backward 1x") > time.sleep(2) > stop() > right() > time.sleep(1) > stop() > print("right 1x") > time.sleep(2) You stopped the right() call before ever doing any output > stop() > fwd() > print("forward 2x") > time.sleep(3) > stop() > left() > time.sleep(1) > print("left 1x") > stop() > fwd() > print("forward 3x") > time.sleep(2) > stop() >------------------------------------------------------ Off-hand, I'd try to get rid of a lot of that redundancy by defining functions to encapsulate your sequence... (Python 2.x syntax -- not tested) (FORWARD, BACKWARD, LEFT, RIGHT) = (1, 2, 3, 4) SEQUENCE = [ (BACKWARD, 2), (RIGHT, 1), (FORWARD, 3), (LEFT, 1), (FORWARD, 2) ] def move(drct, dly): if drct == FORWARD: fwd() print "Forward" elif drct == BACKWARD: bwd() print "Backward" elif drct == LEFT: left() print "Left" elif drct == RIGHT: right() print "Right" else: print "Invalid command: %s" % drct time.sleep(dly) stop() notDone = True servo(90) mindist = 80 while notDone: if mindist > us_dist(15): for (dir, tm) in SEQUENCE: move(dir, tm) -=-=-=-=- Now, if you don't mind having to also press the key, you could use the threading module to handle the keyboard shutdown command instead of using system specific modules to do raw keystroke input. You'd add something before the while loop (and this is really pseudo-code, barely any attempt at Python): import threading def cmdHandler(): global notDone while True: cmd = raw_input("Enter command> ").lower() if cmd.startswith("s"): notDone = False break else: print "Unimplemented command: %s" % cmd cmdThread = threading.Thread(target=cmdHandler) cmdThread.start() This concept doesn't require changes if the underlying OS changes, and does open things up to having more complex commands (one could have commands with arguments since the input is line oriented). -- Wulfraed Dennis Lee Bieber AF6VN wlfraed@ix.netcom.com HTTP://wlfraed.home.netcom.com/