raw_input and break

input/ldompeling at casema.nl input/ldompeling at casema.nl
Fri Nov 6 00:08:59 EST 2015


He,
Thank you for making some time for it.
Is this your code ?
I am also using python 3

I got an error with execute the scipt:
-----------------------------------------------------------------------

Enter command> Traceback (most recent call last):
  File "test06.py", line 44, in <module>
    for (dir, tm) in SEQUENCE:
NameError: name 'SEQUENCE' is not defined
-----------------------------------------------------------------------

import time
import threading

set_right_speed(150)
set_left_speed(105)

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()
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)

---------------------------------------------------------------------------------------------
In reply to "Dennis Lee Bieber" who wrote the following:

> On Thu, 05 Nov 2015 17:28:36 GMT, input/ldompeling at 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 <enter> 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 at ix.netcom.com    HTTP://wlfraed.home.netcom.com/




-- 
--------------------------------- --- -- -
Posted with NewsLeecher v7.0 Beta 2
Web @ http://www.newsleecher.com/?usenet
------------------- ----- ---- -- -




More information about the Python-list mailing list