0

We're currently programming a robot using pygame lib, python, raspberryPI, and adafruit motor hat. Here is example of what we do to increase the speed of the motors from 0 to 100:

for i in range(100): motor.run(i)

This code is inside definitions for moving forward, backward and around and it is used when a keyboard button is pressed. However, we want to interrupt the "for" loop when the KEY is release.

xloayza
  • 11
  • 1
  • 1
    You can break out of a loop using the `break` keyword. You'll need some type of `if ...` check for detecting the key press/release state. – Brendan Abel Jul 07 '16 at 22:22
  • 1
    Don't forget to google around before asking a question. I just googled "python break out of a for loop" and got: https://docs.python.org/2/tutorial/controlflow.html – Checkmate Jul 07 '16 at 22:25

1 Answers1

0

You actually hit the solution on the head with the word 'break':

for i in range(100):
    motor.run(i)
    if ...: #key is released
        break
Checkmate
  • 1,074
  • 9
  • 16