Python Forum
Invalid syntax, but not showing what it is!?!
Thread Rating:
  • 2 Vote(s) - 2 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Invalid syntax, but not showing what it is!?!
#1
Hi guys, I've got a problem ?

I've put some code into idle 3, and it's saying 'syntax error' when I run it, but it isn't showing me where the error is! If I can, I'll post a photo.
I've just re downloaded noobs onto my pi so we're all up to date. The code is for a GPIO intruder alarm circuit.
(Btw I'm not totally sure if some of the code is right, so please correct me if it's wrong!)

Thanks in advance!

Turns out I can't post a pic ( I probably just don't know how) so hopefully I will go on my computer and edit it in now ?
Reply
#2
You have not given us enough information to help you. Please copy and paste the full text that is telling you that there is a syntax error.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
It won't let me edit the original text so here it is:
import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BOARD)
GPIO.setup(7,GPIO.IN)

if GPIO.input(7)==True:
    print ("Secure")

elif: GPIO.input(7)==False
    print ("Intruder alert")
If it doesn't show up, there is a tab indent on the first 'print' and the 'elif' and a double tab before the second 'print'
Reply
#4
(Aug-10-2017, 08:53 PM)WillHampGuy Wrote: elif: GPIO.input(7)==False
elif: is invalid, since there's no condition.  The easy answer is: move the colon to the end of the line.
Reply
#5
Thanks!

And that would fix all the errors?
Reply
#6
All obvious syntax errors, yes.  Perhaps not logic errors.
Reply
#7
Now it's showing a syntax error with the 'elif' highlighted. ??
Reply
#8
if condition:
    # code for this case
elif condition:
    # code for this case
else:
    # code if no case is True
In your case you have the colon at the wrong position.
A condition could be everything which can return a bool

A condition for your if/elif statement can be GPIO.input(7).
You can do also the equality check GPIO.input(7) == True which returns also a boolean.

If I guess, GPIO.input(7) should return an integer object. An integer object with the value 0 it False. If it has the value 1, it's True. 1 == 1.0 == True and 0 == 0.0 == False.

Your fixed code:

import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BOARD)
GPIO.setup(7,GPIO.IN)

if GPIO.input(7)==True:
    print ("Secure")
elif GPIO.input(7)==False:
    print ("Intruder alert")
Or easier:

import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BOARD)
GPIO.setup(7,GPIO.IN)

if GPIO.input(7):
    print ("Secure")
else:
    print ("Intruder alert")
We know that GPIO.input only returns 1 or 0.
There is no third case.

Little hint: If you want to check if an object is a boolean, just check it with bool(your_object). If the object has the special method __bool__, it returns True or False. Then you can use this object in an if/elif statement.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#9
Thanks, but it's still giving me a syntax error!!?? And also can you explain to me how to use a bool please? I tried bool(GPIO.input(7)) btw. It's been a while since I did this!
Reply
#10
You need to post your corrected code, plus the error code in it's entirety, all between the proper code tags. As to bool, DeaD_EyE went into great detail on what it is and how it is used. You might take the time to read the post, if you still have a question, be specific about which part you do not understand.

According to your first post, if you explained it correctly, your code looked like this:

import RPi.GPIO as GPIO
import time
 
GPIO.setmode(GPIO.BOARD)
GPIO.setup(7,GPIO.IN)
 
if GPIO.input(7)==True:
    print ("Secure")
 
    elif: GPIO.input(7)==False
        print ("Intruder alert")
if that is true, than your "elif" is wrong, it should line up with the "if" and the second "print" should line up with the first.
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  print(data) is suddenly invalid syntax db042190 6 1,212 Jun-14-2023, 02:55 PM
Last Post: deanhystad
  SyntaxError: invalid syntax ?? korenron 15 5,785 Jan-25-2022, 11:46 AM
Last Post: korenron
  Invalid syntax with an f-string Mark17 7 7,862 Jan-14-2022, 04:44 PM
Last Post: Mark17
  invalid syntax in my class CompleteNewb 2 1,919 Dec-13-2021, 09:39 AM
Last Post: Larz60+
Exclamation Invalid syntax error(Predict Ethereum Price) lulu43366 2 3,180 Sep-24-2021, 01:24 PM
Last Post: lulu43366
  Unexplained Invalid syntax Error cybertooth 5 3,280 Aug-02-2021, 10:05 AM
Last Post: cybertooth
  [split] SyntaxError: invalid syntax Code_X 3 2,773 May-04-2021, 05:15 PM
Last Post: Yoriz
  Invalid syntax error - need help fixing calgk01 3 3,297 Feb-23-2021, 08:41 PM
Last Post: nilamo
  Invalid syntax using conditionals if - else jperezqu 1 2,345 Jan-13-2021, 07:32 PM
Last Post: bowlofred
  invalid syntax in line 5. Help Asadzangibaloch 2 2,398 Dec-10-2020, 04:26 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020