Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #98244 > unrolled thread

raw_input and break

Started byinput/ldompeling@casema.nl
First post2015-11-04 21:44 +0000
Last post2015-11-06 08:25 -0500
Articles 19 — 7 participants

Back to article view | Back to comp.lang.python


Contents

  raw_input and break input/ldompeling@casema.nl - 2015-11-04 21:44 +0000
    Re: raw_input and break Ian Kelly <ian.g.kelly@gmail.com> - 2015-11-04 14:54 -0700
    Re: raw_input and break Joel Goldstick <joel.goldstick@gmail.com> - 2015-11-04 16:56 -0500
      Re: raw_input and break input/ldompeling@casema.nl - 2015-11-04 22:37 +0000
        Re: raw_input and break Steven D'Aprano <steve@pearwood.info> - 2015-11-05 11:44 +1100
    Re: raw_input and break tian.su.yale@gmail.com - 2015-11-04 21:39 -0800
      Re: raw_input and break input/ldompeling@casema.nl - 2015-11-05 09:22 +0000
        Re: raw_input and break Peter Otten <__peter__@web.de> - 2015-11-05 11:01 +0100
          Re: raw_input and break input/ldompeling@casema.nl - 2015-11-05 10:40 +0000
            Re: raw_input and break Peter Otten <__peter__@web.de> - 2015-11-05 12:38 +0100
              Re: raw_input and break input/ldompeling@casema.nl - 2015-11-05 12:59 +0000
                Re: raw_input and break Peter Otten <__peter__@web.de> - 2015-11-05 15:16 +0100
                  Re: raw_input and break input/ldompeling@casema.nl - 2015-11-05 14:34 +0000
                    Re: raw_input and break Peter Otten <__peter__@web.de> - 2015-11-05 18:06 +0100
                      Re: raw_input and break input/ldompeling@casema.nl - 2015-11-05 17:28 +0000
                        Re: raw_input and break Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2015-11-05 20:09 -0500
                          Re: raw_input and break input/ldompeling@casema.nl - 2015-11-06 05:08 +0000
                            Re: raw_input and break input/ldompeling@casema.nl - 2015-11-06 11:50 +0000
                              Re: raw_input and break Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2015-11-06 08:25 -0500

#98244 — raw_input and break

Frominput/ldompeling@casema.nl
Date2015-11-04 21:44 +0000
Subjectraw_input and break
Message-ID<h%u_x.29450$bN3.2901@fx06.am1>
I have an continues loop with "while True:"
Now I want to use "raw_input" and when I press "s" on the keybord that it will 
"break" the continues loop.

I tried:
choices = raw_input
if choises == s:
break

But even when I not press "s" it "break"
I want that I not press "s" the script continues.

Any ideas ?

Thanks



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

[toc] | [next] | [standalone]


#98245

FromIan Kelly <ian.g.kelly@gmail.com>
Date2015-11-04 14:54 -0700
Message-ID<mailman.29.1446674118.16136.python-list@python.org>
In reply to#98244
On Wed, Nov 4, 2015 at 2:44 PM,  <input/ldompeling@casema.nl> wrote:
> I have an continues loop with "while True:"
> Now I want to use "raw_input" and when I press "s" on the keybord that it will
> "break" the continues loop.
>
> I tried:
> choices = raw_input

This doesn't call raw_input. For that you need to write raw_input(),
where the parentheses indicate a function call. All the above does is
find the raw_input function and assign that function to the name
"choices".

> if choises == s:

Strings in Python are delimited with quotes: "s", not just s. The
latter is going to try to look up a variable named s, not denote a
string. Also, it's important to spell the variable here in the same
way that you did above. Above you have a c where here there is an s.
I'm assuming that this isn't exactly what you ran, because you most
likely would have gotten a NameError here. Please copy and paste
exactly the code that you're trying to run rather than retyping it.

> break

This needs to be indented. Not indenting it would be a SyntaxError, so
again I'm guessing that this isn't actually what you ran.

[toc] | [prev] | [next] | [standalone]


#98246

FromJoel Goldstick <joel.goldstick@gmail.com>
Date2015-11-04 16:56 -0500
Message-ID<mailman.30.1446674207.16136.python-list@python.org>
In reply to#98244
On Wed, Nov 4, 2015 at 4:44 PM, <input/ldompeling@casema.nl> wrote:

> I have an continues loop with "while True:"
> Now I want to use "raw_input" and when I press "s" on the keybord that it
> will
> "break" the continues loop.
>
> I tried:
> choices = raw_input
> if choises == s:
> break
>
> But even when I not press "s" it "break"
> I want that I not press "s" the script continues.
>
> Any ideas ?
>

You need to quote the s:
if choices == 's'

you need to check your spelling -- you have choices and choises

You need to indent break 4 spaces

>
> Thanks
>
>
>
> --
> --------------------------------- --- -- -
> Posted with NewsLeecher v7.0 Beta 2
> Web @ http://www.newsleecher.com/?usenet
> ------------------- ----- ---- -- -
>
> --
> https://mail.python.org/mailman/listinfo/python-list
>



-- 
Joel Goldstick
http://joelgoldstick.com/stats/birthdays

[toc] | [prev] | [next] | [standalone]


#98247

Frominput/ldompeling@casema.nl
Date2015-11-04 22:37 +0000
Message-ID<BMv_x.23349$_R5.7764@fx07.am1>
In reply to#98246
In reply to "Joel Goldstick" who wrote the following:

> On Wed, Nov 4, 2015 at 4:44 PM, <input/ldompeling@casema.nl> wrote:
> 
> > I have an continues loop with "while True:"
> > Now I want to use "raw_input" and when I press "s" on the keybord that it
> > will
> > "break" the continues loop.
> > 
> > I tried:
> > choices = raw_input
> > if choises == s:
> > break
> > 
 You need to quote the s:
> if choices == 's'

I quote the s, but its still break even when I not press the s.

--------------------------------------------------------------

>But even when I not press "s" it "break"
> > I want that I not press "s" the script continues.
> > 
> > Any ideas ?
> > 
> 
> You need to quote the s:
> if choices == 's'
> 
> you need to check your spelling -- you have choices and choises
> 
> You need to indent break 4 spaces
> 
> > 
> > Thanks
> > 
> > 
> > 
> > --
> > --------------------------------- --- -- -
> > Posted with NewsLeecher v7.0 Beta 2
> > Web @ http://www.newsleecher.com/?usenet
> > ------------------- ----- ---- -- -
> > 
> > --
> > https://mail.python.org/mailman/listinfo/python-list
> > 
> 
> 
> 
> --
> Joel Goldstick
> http://joelgoldstick.com/stats/birthdays




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

[toc] | [prev] | [next] | [standalone]


#98262

FromSteven D'Aprano <steve@pearwood.info>
Date2015-11-05 11:44 +1100
Message-ID<563aa679$0$1608$c3e8da3$5496439d@news.astraweb.com>
In reply to#98247
On Thu, 5 Nov 2015 09:37 am, input/ldompeling@casema.nl wrote:

> I quote the s, but its still break even when I not press the s.

The code you have shown us is so full of bugs and typos that you cannot
possibly be running that code.

Unfortunately, we do not have magical powers. We cannot see the code you
*actually* run, only the code you show us. So if you want help, you need to
show us the code you actually run.

COPY AND PASTE -- do not retype from memory -- the while loop from your
code. Make sure it will run on its own. If you can't run the code, because
it lacks variables or something else, neither can we.

Help us to help you. We cannot help you unless you show us the code you are
running.



-- 
Steven

[toc] | [prev] | [next] | [standalone]


#98278

Fromtian.su.yale@gmail.com
Date2015-11-04 21:39 -0800
Message-ID<4270fade-5f09-4935-a99a-f2be344202b2@googlegroups.com>
In reply to#98244
在 2015年11月4日星期三 UTC-6下午3:45:09,input/ld...@casema.nl写道:
> I have an continues loop with "while True:"
> Now I want to use "raw_input" and when I press "s" on the keybord that it will 
> "break" the continues loop.
> 
> I tried:
> choices = raw_input
> if choises == s:
> break
> 
> But even when I not press "s" it "break"
> I want that I not press "s" the script continues.
> 
> Any ideas ?
> 
> Thanks
> 
> 
> 
> -- 
> --------------------------------- --- -- -
> Posted with NewsLeecher v7.0 Beta 2
> Web @ http://www.newsleecher.com/?usenet
> ------------------- ----- ---- -- -

while True:
    inp = input("Enter whatever you want, letter 's' to stop: ")
    if inp == 's':
        print("Program stopped.")
        break
    print("You just entered:", inp)

Is this along the line that you are thinking? As pointed out earlier, it will be much easier for people to help if you can share the exact code you put into and the error messages as well. Thanks and good luck!
All the best,
Tian

[toc] | [prev] | [next] | [standalone]


#98293

Frominput/ldompeling@casema.nl
Date2015-11-05 09:22 +0000
Message-ID<adF_x.29624$Xj7.21693@fe35.am1>
In reply to#98278
In reply to "tian.su.yale@gmail.com" who wrote the following:

> =E5=9C=A8 2015=E5=B9=B411=E6=9C=884=E6=97=A5=E6=98=9F=E6=9C=9F=E4=B8=89 UTC=
> -6=E4=B8=8B=E5=8D=883:45:09=EF=BC=8Cinput/ld...@casema.nl=E5=86=99=E9=81=93=
> =EF=BC=9A
> > I have an continues loop with "while True:"
> > Now I want to use "raw_input" and when I press "s" on the keybord that it=
>  will=20
> > "break" the continues loop.
In this script it always break even when I not  press 's'
I want that this script go's to if mindist > us_dist(15):
And when I press 's' its stop.

Any ideas ?

--------------------------------------------------------


while True:
    enable_servo()
    servo(90)
    mindist = 80
    choices = input("letter s to stop:")
    if choices == 's':
        print ("stop")
        break
if mindist > us_dist(15):
    bwd()
    print ("backward 1x")

    














> > =20
> > I tried:
> > choices =3D raw_input
> > if choises =3D=3D s:
> > break
> > =20
> > But even when I not press "s" it "break"
> > I want that I not press "s" the script continues.
> > =20
> > Any ideas ?
> > =20
> > Thanks
> > =20
> > =20
> > =20
> > --=20
> > --------------------------------- --- -- -
> > Posted with NewsLeecher v7.0 Beta 2
> > Web @ http://www.newsleecher.com/?usenet
> > ------------------- ----- ---- -- -
> 
> while True:
>     inp =3D input("Enter whatever you want, letter 's' to stop: ")
>     if inp =3D=3D 's':
>         print("Program stopped.")
>         break
>     print("You just entered:", inp)
> 
> Is this along the line that you are thinking? As pointed out earlier, it wi=
> ll be much easier for people to help if you can share the exact code you pu=
> t into and the error messages as well. Thanks and good luck!
> All the best,
> Tian




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

[toc] | [prev] | [next] | [standalone]


#98294

FromPeter Otten <__peter__@web.de>
Date2015-11-05 11:01 +0100
Message-ID<mailman.50.1446717724.16136.python-list@python.org>
In reply to#98293
input/ldompeling@casema.nl wrote:

> while True:
>     enable_servo()
>     servo(90)
>     mindist = 80
>     choices = input("letter s to stop:")
>     if choices == 's':
>         print ("stop")
>         break
> if mindist > us_dist(15):
>     bwd()
>     print ("backward 1x")

> In this script it always break even when I not  press 's'
> I want that this script go's to if mindist > us_dist(15):
> And when I press 's' its stop.
> 
> Any ideas ?

Does it print something like

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<string>", line 0
    
    ^
SyntaxError: unexpected EOF while parsing

when it stops? This is called a "traceback" and you should always include it 
in your post when you need help with an error in your script.

In this particular case the problem is that you are using Python 2 where 
input() tries to execute the text the user enters as Python code.

Tian's example code assumes Python 3.

To fix your problem replace the line

    choices = input("letter s to stop:")

in your script with

    choices = raw_input("letter s to stop:")

[toc] | [prev] | [next] | [standalone]


#98295

Frominput/ldompeling@casema.nl
Date2015-11-05 10:40 +0000
Message-ID<rmG_x.48746$kZ5.30605@fe42.am1>
In reply to#98294
In reply to "Peter Otten" who wrote the following:

> input/ldompeling@casema.nl wrote:
> 
> > while True:
> >     enable_servo()
> >     servo(90)
> >     mindist = 80
> >     choices = input("letter s to stop:")
> >     if choices == 's':
> >         print ("stop")
> >         break
> > if mindist > us_dist(15):
>In this particular case the problem is that you are using Python 2 where
>input() tries to execute the text the user enters as Python code.

>Tian's example code assumes Python 3.

>To fix your problem replace the line

 >   choices = input("letter s to stop:")

>in your script with

 >   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.

Thanks
--------------------------------------------------------------------------------------------------------------------------















> >     bwd()
> >     print ("backward 1x")
> 
> > In this script it always break even when I not  press 's'
> > I want that this script go's to if mindist > us_dist(15):
> > And when I press 's' its stop.
> > 
> > Any ideas ?
> 
> Does it print something like
> 
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
>   File "<string>", line 0
> 
>     ^
> SyntaxError: unexpected EOF while parsing
> 
> when it stops? This is called a "traceback" and you should always include it
> in your post when you need help with an error in your script.
> 
> In this particular case the problem is that you are using Python 2 where
> input() tries to execute the text the user enters as Python code.
> 
> Tian's example code assumes Python 3.
> 
> To fix your problem replace the line
> 
>     choices = input("letter s to stop:")
> 
> in your script with
> 
>     choices = raw_input("letter s to stop:")




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

[toc] | [prev] | [next] | [standalone]


#98296

FromPeter Otten <__peter__@web.de>
Date2015-11-05 12:38 +0100
Message-ID<mailman.51.1446723514.16136.python-list@python.org>
In reply to#98295
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")

[toc] | [prev] | [next] | [standalone]


#98298

Frominput/ldompeling@casema.nl
Date2015-11-05 12:59 +0000
Message-ID<0pI_x.14172$aq1.6942@fe58.am1>
In reply to#98296
In reply to "Peter Otten" who wrote the following:

> 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)
Thanks for the reply. Is there no easy way to do so in python ?
I am using the raspberry pi with Wheezy and for the robot the GoPiGo which is connected on the raspberry pi.
The GoPiGo board has his own code like: fwd()="forward" bwd()="backward" right()="right" left="left" and so on.
I am just a dummie with python.

Thanks
----------------------------------------------------------

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




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

[toc] | [prev] | [next] | [standalone]


#98304

FromPeter Otten <__peter__@web.de>
Date2015-11-05 15:16 +0100
Message-ID<mailman.54.1446733002.16136.python-list@python.org>
In reply to#98298
input/ldompeling@casema.nl wrote:

> In reply to "Peter Otten" who wrote the following:
> 
>> 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)
> Thanks for the reply. Is there no easy way to do so in python ?

If you are asking for a ready-made function in the standard library, I don't 
know one and I don't think there is one (there are recipes that work on top 
of curses though).

> I am using the raspberry pi with Wheezy and for the robot the GoPiGo which
> is connected on the raspberry pi. The GoPiGo board has his own code like:
> fwd()="forward" bwd()="backward" right()="right" left="left" and so on. I
> am just a dummie with python.

The code in capture_key.py may look a bit scary, but just as I took it 
without bothering the details you can take the resulting module without 
caring about the code in it. Alternatively you can search 

https://pypi.python.org

for a solution that you prefer.

[toc] | [prev] | [next] | [standalone]


#98307

Frominput/ldompeling@casema.nl
Date2015-11-05 14:34 +0000
Message-ID<TNJ_x.13318$aD7.1426@fe55.am1>
In reply to#98304
>The code in capture_key.py may look a bit scary, but just as I took it
>without bothering the details you can take the resulting module without
>caring about the code in it. Alternatively you can search

>https://pypi.python.org

Thanks for the link. I realy appreciate it.
Can you also tell me what I am looking for in https://pypi.python.org for a result.

Thanks
 















In reply to "Peter Otten" who wrote the following:

> input/ldompeling@casema.nl wrote:
> 
> > In reply to "Peter Otten" who wrote the following:
> > 
> > > 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)
> > Thanks for the reply. Is there no easy way to do so in python ?
> 
> If you are asking for a ready-made function in the standard library, I don't
> know one and I don't think there is one (there are recipes that work on top
> of curses though).
> 
> > I am using the raspberry pi with Wheezy and for the robot the GoPiGo which
> > is connected on the raspberry pi. The GoPiGo board has his own code like:
> > fwd()="forward" bwd()="backward" right()="right" left="left" and so on. I
> > am just a dummie with python.
> 
> The code in capture_key.py may look a bit scary, but just as I took it
> without bothering the details you can take the resulting module without
> caring about the code in it. Alternatively you can search
> 
> https://pypi.python.org
> 
> for a solution that you prefer.




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

[toc] | [prev] | [next] | [standalone]


#98311

FromPeter Otten <__peter__@web.de>
Date2015-11-05 18:06 +0100
Message-ID<mailman.58.1446743214.16136.python-list@python.org>
In reply to#98307
input/ldompeling@casema.nl wrote:

>>The code in capture_key.py may look a bit scary, but just as I took it
>>without bothering the details you can take the resulting module without
>>caring about the code in it. Alternatively you can search
> 
>>https://pypi.python.org
> 
> Thanks for the link. I realy appreciate it.
> Can you also tell me what I am looking for in https://pypi.python.org for
> a result.

I didn't have a particular module in mind. Pypy, or the "Python Package 
Index", is a repository for all kinds of modules and libraries. If you don't 
yet feel comfortable looking around (peferrably using the search engine of 
your choice) and experimenting a bit I recommend that you try the piece of 
code I posted (or wait for someone else to chime in with a better 
suggestion).

[toc] | [prev] | [next] | [standalone]


#98313

Frominput/ldompeling@casema.nl
Date2015-11-05 17:28 +0000
Message-ID<8lM_x.24516$cf7.4240@fx02.am1>
In reply to#98311
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)
enable_servo()
fwd()
print("forward 1x")
time.sleep(4)
stop()

while True:
    servo(90)
    mindist = 80

    if mindist > us_dist(15):
        bwd()
        print ("backward 1x")
        time.sleep(2)
        stop()
        right()
        time.sleep(1)
        stop()
        print("right 1x")
        time.sleep(2)
        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()
------------------------------------------------------
In reply to "Peter Otten" who wrote the following:

> input/ldompeling@casema.nl wrote:
> 
> > > The code in capture_key.py may look a bit scary, but just as I took it
> > > without bothering the details you can take the resulting module without
> > > caring about the code in it. Alternatively you can search
> > 
> > > https://pypi.python.org
> > 
> > Thanks for the link. I realy appreciate it.
> > Can you also tell me what I am looking for in https://pypi.python.org for
> > a result.
> 
> I didn't have a particular module in mind. Pypy, or the "Python Package
> Index", is a repository for all kinds of modules and libraries. If you don't
> yet feel comfortable looking around (peferrably using the search engine of
> your choice) and experimenting a bit I recommend that you try the piece of
> code I posted (or wait for someone else to chime in with a better
> suggestion).




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

[toc] | [prev] | [next] | [standalone]


#98324

FromDennis Lee Bieber <wlfraed@ix.netcom.com>
Date2015-11-05 20:09 -0500
Message-ID<mailman.66.1446772190.16136.python-list@python.org>
In reply to#98313
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 <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@ix.netcom.com    HTTP://wlfraed.home.netcom.com/

[toc] | [prev] | [next] | [standalone]


#98330

Frominput/ldompeling@casema.nl
Date2015-11-06 05:08 +0000
Message-ID<LBW_x.37419$pB.28644@fe37.am1>
In reply to#98324
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@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@ix.netcom.com    HTTP://wlfraed.home.netcom.com/




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

[toc] | [prev] | [next] | [standalone]


#98347

Frominput/ldompeling@casema.nl
Date2015-11-06 11:50 +0000
Message-ID<uu0%x.51136$Mw6.36660@fx03.am1>
In reply to#98330
Hi,

I tried to define SEQUENCE with:
The script is working now, exept that I do'nt get a menu in terminal for "s" to stop.
---------------------------------------------------------------------------------

def SEQUENCE():


    while notDone:
        if mindist > us_dist(15):
            for (dir, tm) in SEQUENCE:
                    move(dir, tm)

---------------------------------------------------------------------------------
In reply to "input/ldompeling@casema.nl" who wrote the following:

> 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@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@ix.netcom.com    HTTP://wlfraed.home.netcom.com/
> 
> 
> 
> 
> --
> --------------------------------- --- -- -
> Posted with NewsLeecher v7.0 Beta 2
> Web @ http://www.newsleecher.com/?usenet
> ------------------- ----- ---- -- -




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

[toc] | [prev] | [next] | [standalone]


#98352

FromDennis Lee Bieber <wlfraed@ix.netcom.com>
Date2015-11-06 08:25 -0500
Message-ID<mailman.85.1446816343.16136.python-list@python.org>
In reply to#98347
On Fri, 06 Nov 2015 11:50:50 GMT, input/ldompeling@casema.nl declaimed the
following:

>Hi,
>
>I tried to define SEQUENCE with:
>The script is working now, exept that I do'nt get a menu in terminal for "s" to stop.
>---------------------------------------------------------------------------------
>
>def SEQUENCE():
>
	My untested/pseudo-code DID define SEQUENCE -- but you skipped right
over it when copying it into your file. (from below, before I trimmed)

>> > 
>> > 
>> > (FORWARD, BACKWARD, LEFT, RIGHT) = (1, 2, 3, 4)
>> > SEQUENCE = [ (BACKWARD, 2),
>> >     (RIGHT, 1),
>> >     (FORWARD, 3),
>> >     (LEFT, 1),
>> >     (FORWARD, 2) ]
>> > 


	As for a prompt for stopping -- all that was in the "advanced"
pseudo-code using the threading module. You'll have to read the relevant
sections of the library reference manual to figure out what it is doing
(though that is a very simple usage -- normally I'd make the main program
the controller, and use Queue() to create a channel to the thread rather
than using a global name).

	I'm not trying to produce a working program, but only to provide
snippets showing alternative, more maintainable, designs. Changing the
actions of the "robot" in your linear design would require replacing most
of your program -- whereas my example only needs to modify SEQUENCE itself
(and SEQUENCE could easily be replaced by reading lines from a data file).
-- 
	Wulfraed                 Dennis Lee Bieber         AF6VN
    wlfraed@ix.netcom.com    HTTP://wlfraed.home.netcom.com/

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web