nautilus27
Posts: 72
Joined: Fri May 22, 2015 9:38 pm

Syntax error in if code

Sat Sep 24, 2016 12:10 pm

Hey peeps,

Last year I made a hovercraft with Arduino and used skid steering via 2x motors. It worked great and controls were pretty accurate. So I'm trying to convert it to Python. I'm using Xbox 360 controller with xboxdrv drivers. Analog stick feedbacks are converted to certain values. Everything went smooth until i get syntax error for the if code:

Code: Select all

  File "sample.py", line 55
    if left_y_1023 > 550
                    ^
SyntaxError: invalid syntax
Why is this happening?

Code: Select all


import xbox
import RPi.GPIO as GPIO
import time

def mymap(x, in_min, in_max, out_min, out_max):
    return int((x-in_min) * (out_max-out_min) / (in_max-in_min) + out_min)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(11,GPIO.OUT)
GPIO.setup(12,GPIO.OUT)
GPIO.setup(15,GPIO.OUT)
GPIO.setup(16,GPIO.OUT)

motor1 = GPIO.PWM(11,50)
motor2 = GPIO.PWM(12,50)
motor3 = GPIO.PWM(15,50)
motor4 = GPIO.PWM(16,50)


while not joy.Back():

    left_x_1023 = mymap(joy.leftX(),-1.0,1.0,0,1023)
    print(left_x_1023)," ",
    left_y_1023 = mymap(joy.leftY(),-1.0,1.0,0,1023)
    print(left_y_1023), " ",
    right_x_1023 = mymap(joy.rightX(),-1.0,1.0,0,1023)
    print(right_x_1023), " ",
    right_y_1023 = mymap(joy.rightY(),-1.0,1.0,0,1023)
    print(right_y_1023), " ",

    speed1 = mymap(left_y_1023, 550,1023,0,100)
    speed2 = mymap(left_y_1023, 500,0,0,100)
    
    turn1 = mymap(left_x_1023,500,0,-50,50)

    skid1a = (speed1 - turn1)
    skid1b = (speed1 + turn1)
    
    turn2 = mymap(left_x_1023,550,1023,-50,50)
    
    skid2a = (speed1 - turn2)
    skid2b = (speed1 + turn2)
    
    skid3a = (speed2 - turn1)
    skid3b = (speed2 + turn1)
    
    skid4a = (speed2 - turn2)
    skid4b = (speed2 + turn2)
    
    turn3 = mymap(left_x_1023,500,0,0,100)
    turn4 = mymap(left_x_1023,550,1023,0,100)
    
    if left_y_1023 > 550
        if left_x_1023 > 501 and left_x_1023 < 549
            motor1.start(speed1)
            motor2.start(0)
            motor3.start(speed1)
            motor4.start(0)

User avatar
joan
Posts: 14747
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Syntax error in if code

Sat Sep 24, 2016 12:14 pm

Python requires a colon at the start of a block.

See http://stackoverflow.com/questions/2155 ... -in-python
Last edited by joan on Sat Sep 24, 2016 12:16 pm, edited 1 time in total.

User avatar
kusti8
Posts: 3439
Joined: Sat Dec 21, 2013 5:29 pm
Location: USA

Re: Syntax error in if code

Sat Sep 24, 2016 12:14 pm

You don't have a colon after the if statement
There are 10 types of people: those who understand binary and those who don't.

nautilus27
Posts: 72
Joined: Fri May 22, 2015 9:38 pm

Re: Syntax error in if code

Sat Sep 24, 2016 12:16 pm

joan wrote:Python requires a colon at the end of the if statement.
Thanks! I'm such a Python noob really. I wish i knew a Python Language reference like Arduino Referance in Arduino website.

https://www.arduino.cc/en/Reference/HomePage

Is there any?

User avatar
joan
Posts: 14747
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Syntax error in if code

Sat Sep 24, 2016 12:17 pm

nautilus27 wrote:
joan wrote:Python requires a colon at the end of the if statement.
Thanks! I'm such a Python noob really. I wish i knew a Python Language reference like Arduino Referance in Arduino website.

https://www.arduino.cc/en/Reference/HomePage

Is there any?
Just search for a Python tutorial with Google. You should find one which chimes with you.

User avatar
kusti8
Posts: 3439
Joined: Sat Dec 21, 2013 5:29 pm
Location: USA

Re: Syntax error in if code

Sat Sep 24, 2016 12:22 pm

Here's the documentation:
https://docs.python.org/3/
There are 10 types of people: those who understand binary and those who don't.

Return to “Python”