Python3 SyntaxError: expected an indented block

Python3 SyntaxError: expected an indented block

我希望我的程序是: 当我输入“3”键从主菜单选择 > 'N' 键到 return 回到主菜单选择。

但现在

状态为 "the expected an indented block"
def Menu():

这是我的完整编码:

import RPi.GPIO as GPIO
import Slush
import math
import time
import sys

from inputs import get_key
from time import sleep


b = Slush.sBoard()
m = [Slush.Motor(0), Slush.Motor(1), Slush.Motor(2), Slush.Motor(3)]

m[0].setMaxSpeed(150)
m[1].setMaxSpeed(150)
m[2].setMaxSpeed(250)
m[3].setMaxSpeed(150)

m[0].setCurrent(150, 150, 150, 150)
m[1].setCurrent(100, 100, 100, 100)
m[2].setCurrent(150, 150, 150, 150)
m[3].setCurrent(100, 100, 100, 100)


GPIO.setmode(GPIO.BCM)
GPIO.setup(18, GPIO.OUT)
pwm = GPIO.PWM(18, 50)
pwm.start(0)
GPIO.output(18, 1)


def Menu():
print("Please Enter Number From List Below:")
print("1: Start Program")
print("2: Control Manually")
print("3: Exit Program")
inputkey = int(input("Enter a number & Press Enter:"))


#START PROGRAM
if inputkey == 1:
    print("Press Home Key To Start Program")
#def_StartProgram()

    while 1:
        events = get_key()
        for event in events:
                if event.code == 'KEY_HOME':
                    print("PROGRAM START")
                    print("\nMotor0 Position is", m[0].getPosition())
                    print("\nMotor1 Position is", m[1].getPosition())
                    print("\nMotor2 Position is", m[2].getPosition())
                    print("\nMotor3 Position is", m[3].getPosition())
                    m[1].goTo(-10000)
                    time.sleep(3)
                    print("\nMotor1 Position is", m[1].getPosition())


#CONTROL MANUAL
if inputkey == 2:
    print("Manual Mode")

    while 1:
        events = get_key()
        for event in events:

            if event.code == 'KEY_Q':
                value = event.state
                if value == 1:
                    if not m[0].isBusy(): m[0].run(1, 35)
            if event.code == 'KEY_A':
                value = event.state
                if value == 1:
                    if not m[0].isBusy(): m[0].softStop()
            if event.code == 'KEY_Z':
                value = event.state
                if value == 1:
                    if not m[0].isBusy(): m[0].run(0, 35)



            if event.code == 'KEY_W':
                value = event.state
                if value == 1:
                    if not m[1].isBusy(): m[1].run(1, 20)
            if event.code == 'KEY_S':
                value = event.state
                if value == 1:
                    if not m[1].isBusy(): m[1].softStop()
            if event.code == 'KEY_X':
                value = event.state
                if value == 1:
                    if not m[1].isBusy(): m[1].run(0, 20)



            if event.code == 'KEY_E':
                value = event.state
                if value == 1:
                    if not m[2].isBusy(): m[2].run(1, 100)
            if event.code == 'KEY_D':
                value = event.state
                if value == 1:
                    if not m[2].isBusy(): m[2].softStop()
            if event.code == 'KEY_C':
                value = event.state
                if value == 1:
                    if not m[2].isBusy(): m[2].run(0, 100)




            if event.code == 'KEY_R':
                value = event.state
                if value == 1:
                    if not m[3].isBusy(): m[3].run(1, 10)
            if event.code == 'KEY_F':
                value = event.state
                if value == 1:
                    if not m[3].isBusy(): m[3].softStop()
            if event.code == 'KEY_V':
                value = event.state
                if value == 1:
                    if not m[3].isBusy(): m[3].run(0, 10)



            if event.code == 'KEY_1':
                value = event.state
                if value == 1:
                    pwm.ChangeDutyCycle(1)
                    sleep(0.5)
                    if not pwm.stop(): pwm.start(0)


            if event.code == 'KEY_3':
                value = event.state
                if value == 1:
                    if not pwm.ChangeDutyCycle(0): pwm.ChangeDutyCycle(99)

#EXIT PROGRAM
if inputkey == 3:
    print("Press 'Y/N' key = Exit/Back To Menu ")


    while 1:
        events = get_key()
        for event in events:

            if event.code == 'KEY_Y':
                sys.exit(0)
            if event.code == 'KEY_N':
                Menu()

这就是我希望我的程序运行的方式。流程图: enter image description here

您在 def 菜单 中遗漏了缩进。请尝试下面的代码,因为我不知道你的确切逻辑,所以程序可能无法按你的需要运行,但你会对逻辑有一个很好的了解。

import RPi.GPIO as GPIO
import Slush
import math
import time
import sys

from inputs import get_key
from time import sleep


b = Slush.sBoard()
m = [Slush.Motor(0), Slush.Motor(1), Slush.Motor(2), Slush.Motor(3)]

m[0].setMaxSpeed(150)
m[1].setMaxSpeed(150)
m[2].setMaxSpeed(250)
m[3].setMaxSpeed(150)

m[0].setCurrent(150, 150, 150, 150)
m[1].setCurrent(100, 100, 100, 100)
m[2].setCurrent(150, 150, 150, 150)
m[3].setCurrent(100, 100, 100, 100)


GPIO.setmode(GPIO.BCM)
GPIO.setup(18, GPIO.OUT)
pwm = GPIO.PWM(18, 50)
pwm.start(0)
GPIO.output(18, 1)

def Menu():
    while 1:    
        print("Please Enter Number From List Below:")
        print("1: Start Program")
        print("2: Control Manually")
        print("3: Exit Program")
        inputkey = int(input("Enter a number & Press Enter:"))


        #START PROGRAM
        if inputkey == 1:
            print("Press Home Key To Start Program")
        #def_StartProgram()

            while 1:
                events = get_key()
                for event in events:
                        if event.code == 'KEY_HOME':
                            print("PROGRAM START")
                            print("\nMotor0 Position is", m[0].getPosition())
                            print("\nMotor1 Position is", m[1].getPosition())
                            print("\nMotor2 Position is", m[2].getPosition())
                            print("\nMotor3 Position is", m[3].getPosition())
                            m[1].goTo(-10000)
                            time.sleep(3)
                            print("\nMotor1 Position is", m[1].getPosition())


        #CONTROL MANUAL
        if inputkey == 2:
            print("Manual Mode")

            while 1:
                events = get_key()
                for event in events:

                    if event.code == 'KEY_Q':
                        value = event.state
                        if value == 1:
                            if not m[0].isBusy(): m[0].run(1, 35)
                    if event.code == 'KEY_A':
                        value = event.state
                        if value == 1:
                            if not m[0].isBusy(): m[0].softStop()
                    if event.code == 'KEY_Z':
                        value = event.state
                        if value == 1:
                            if not m[0].isBusy(): m[0].run(0, 35)



                    if event.code == 'KEY_W':
                        value = event.state
                        if value == 1:
                            if not m[1].isBusy(): m[1].run(1, 20)
                    if event.code == 'KEY_S':
                        value = event.state
                        if value == 1:
                            if not m[1].isBusy(): m[1].softStop()
                    if event.code == 'KEY_X':
                        value = event.state
                        if value == 1:
                            if not m[1].isBusy(): m[1].run(0, 20)



                    if event.code == 'KEY_E':
                        value = event.state
                        if value == 1:
                            if not m[2].isBusy(): m[2].run(1, 100)
                    if event.code == 'KEY_D':
                        value = event.state
                        if value == 1:
                            if not m[2].isBusy(): m[2].softStop()
                    if event.code == 'KEY_C':
                        value = event.state
                        if value == 1:
                            if not m[2].isBusy(): m[2].run(0, 100)




                    if event.code == 'KEY_R':
                        value = event.state
                        if value == 1:
                            if not m[3].isBusy(): m[3].run(1, 10)
                    if event.code == 'KEY_F':
                        value = event.state
                        if value == 1:
                            if not m[3].isBusy(): m[3].softStop()
                    if event.code == 'KEY_V':
                        value = event.state
                        if value == 1:
                            if not m[3].isBusy(): m[3].run(0, 10)



                    if event.code == 'KEY_1':
                        value = event.state
                        if value == 1:
                            pwm.ChangeDutyCycle(1)
                            sleep(0.5)
                            if not pwm.stop(): pwm.start(0)


                    if event.code == 'KEY_3':
                        value = event.state
                        if value == 1:
                            if not pwm.ChangeDutyCycle(0): pwm.ChangeDutyCycle(99)

        #EXIT PROGRAM
        if inputkey == 3:
            print("Press 'Y/N' key = Exit/Back To Menu ")

            exit_flag = True
            while exit_flag:
                events = get_key()
                for event in events:

                    if event.code == 'KEY_Y':
                        sys.exit(0)
                    elif event.code == 'KEY_N':
                        exit_flag = False
                        break
Menu()