raw_input and break

Peter Otten __peter__ at web.de
Thu Nov 5 05:01:45 EST 2015


input/ldompeling at 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:")





More information about the Python-list mailing list