small medium large xlarge

24 Mar 2014, 18:57
Saad Bashir (1 post)

1.On page 90 and 91 while discussing if and elif statements, when I type elif in the python shell, it says ‘invalid syntax’ and highlights “elif.”

I am using the 3.2.3 version of Python.

Can anyone help please?

25 Mar 2014, 16:52
Jason Montojo (8 posts)

Hi Saad,

Can you paste exactly what you’re typing into the shell to get the ‘invalid syntax’ error? ‘elif’ needs to be used in conjunction with ‘if’ so if you’re missing the ‘if’, that’s one reason why you’re getting the error. Or if you terminate the ‘if’ block too early by adding a blank line after the ‘if’ block.

Hope this helps, Jason

07 Dec 2016, 01:23
Farai Kufakwedu (1 post)

I am getting the same SyntaxError message with the code below when i use the elif command. Once I type the : after the 7.0 and press enter, the error message pops up. what am I doing wrong here?

ph = float(input(“Enter your pH here: “)) Enter your pH here: 8.9 if ph > 7.0: print(ph, “is acidic”) elif ph < 7.0:

SyntaxError: invalid syntax

08 Jan 2017, 12:33
Jennifer Campbell (17 posts)

It looks like your elif statement may be missing the code in the body. Here is an example:

ph = float(input(“Enter your pH here: “))
if ph > 7.0: 
    print(ph, “is acidic”) 
elif ph < 7.0:
    # Put some code here, for example:
    print(ph, "not acidic")
You must be logged in to comment