EpixYaz
Posts: 5
Joined: Sun Jun 14, 2015 10:46 am

Syntax errors

Mon Jun 22, 2015 12:08 pm

Hi guys, I am trying to debug my entry for the competition but I keep getting syntax errors which I can't seem to correct. I have tried to look at tutorials for the statements I am trying to produce but they say that syntax-wise the code is correct.
The first part of the code (setting up all the modules and such), works perfectly without error. However when it starts the while statements, it all goes wrong :lol: It is supposed to measure the variables and put them on lines of the LED matrix according to the percentage of the set 'highest' value, e.g if it was between 12.5% and 25% then it would show up to be green on the first row. I haven't got around to commenting on code yet but p25,p375 etc are pre-set patterns on the matrix.

Thanks in advance!

Code: Select all

while lightValue < 20000:
    control.key = input(ap.show_message("Good evening Tim! Use the joystick to select a variable to measure: Left = Temperature Right = Humidity and Up = Pressure", text_colour=[208, 108, 245])
    if control.key == K_LEFT:
        if temp < 5: # Temperature is measured in degrees celsius
            ap.set_pixels(redlow)
        while temp >= 5 and <= 7:
            ap.set_pixels(p125)
        while temp >= 8 and <= 11:
            ap.set_pixels(p25)
        while temp >= 12 and <= 15:
            ap.set_pixels(p375)
        while temp >= 16 and <= 18:
            ap.set_pixels(p50)
        while temp >= 19 and <= 22:
            ap.set_pixels(p625)
        while temp >= 23 and <= 26:
            ap.set_pixels(p75)
        while temp >= 27 and < 30:
            ap.set_pixels(p875)
        while temp == 30:
            ap.set_pixels(p100)
        while temp > 30:
        ap.set_pixels(redhigh)

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

Re: Syntax errors

Mon Jun 22, 2015 12:30 pm

The while statement loops while something is true.

Code: Select all

x=5
while x == 5:
   print("yes, x is 5")
will continually print yes, x is 5

Contrast with

Code: Select all

x=5
if x == 5:
   print("yes, x is 5")
which will print yes, x is 5 and then continue at the next statement.

User avatar
jojopi
Posts: 3233
Joined: Tue Oct 11, 2011 8:38 pm

Re: Syntax errors

Mon Jun 22, 2015 12:32 pm

When Python detects a syntax error, it will show you the line of code that it was reading at the time, with a ^ caret pointing out the part where it became confused. It is important to pay attention to this. The actual mistake may not be on the line identified, but it will usually be just before.

The problem at the "if control.key == K_LEFT:" line is that the line above has two (( and only one ), so we are inside an unterminated parenthesis. This error cannot be detected until the ":" at the end of the reporting line, because in Python you are allowed to start a new line inside a parenthesis, and you are also allowed to append "if CONDITION else EXPRESSION2" to an expression.

Another problem is that you cannot write "and <= 7". You can either use mathematical style "5 <= temp <= 7", or traditional programming style "temp >= 5 and temp <= 7".

EpixYaz
Posts: 5
Joined: Sun Jun 14, 2015 10:46 am

Re: Syntax errors

Mon Jun 22, 2015 3:32 pm

Thanks so much guys! I got it to work

Return to “Astro Pi”