Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/350.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何为该程序编写循环,使其每次返回顶部?_Python_Python 2.7 - Fatal编程技术网

Python 如何为该程序编写循环,使其每次返回顶部?

Python 如何为该程序编写循环,使其每次返回顶部?,python,python-2.7,Python,Python 2.7,这是我正在使用的代码。这是使步进电机旋转,但每次通过代码后,它完全停止。我怎样才能让代码重新开始 import RPi.GPIO as GPIO, time GPIO.setmode(GPIO.BOARD) GPIO.setup(16, GPIO.OUT) GPIO.setup(18, GPIO.OUT) p = GPIO.PWM(16, 500) def SpinMotor(direction, num_steps): GPIO.output(18, direction)

这是我正在使用的代码。这是使步进电机旋转,但每次通过代码后,它完全停止。我怎样才能让代码重新开始

import RPi.GPIO as GPIO, time

GPIO.setmode(GPIO.BOARD)
GPIO.setup(16, GPIO.OUT)
GPIO.setup(18, GPIO.OUT)
p = GPIO.PWM(16, 500)


def SpinMotor(direction, num_steps):
    GPIO.output(18, direction)
    while num_steps > 0:
        p.start(1)
        time.sleep(0.01)
        num_steps -= 1
    p.stop()
    GPIO.cleanup()
    return True

direction_input = raw_input('Please enter O or C for Open or Close')
num_steps = input('Please enter the number of steps:')
if direction_input == 'O':
    SpinMotor(True, num_steps)

else:
    SpinMotor(False, num_steps)

您可以创建如下循环:

while True:
    direction_input = raw_input('Please enter O or C for Open or Close. Q to quit.')
    if direction_input == 'Q':
        break
    num_steps = input('Please enter the number of steps:')
    if direction_input == 'O':
        SpinMotor(True, num_steps)

    else:
        SpinMotor(False, num_steps)

为True时: