22

I'm a newbie to Python and currently learning Control Flow commands like if, else, etc.

The if statement is working all fine, but when I write else or elif commands, the interpreter gives me a syntax error. I'm using Python 3.2.1 and the problem is arising in both its native interpreter and IDLE.

I'm following as it is given in the book 'A Byte Of Python' . As you can see, elif and else are giving Invalid Syntax.

>> number=23
>> guess = input('Enter a number : ')
>> if guess == number:
>>    print('Congratulations! You guessed it.')
>> elif guess < number:
   **( It is giving me 'Invalid Syntax')**
>> else:
   **( It is also giving me 'Invalid syntax')**

Why is this happening? I'm getting the problem in both IDLE and the interactive python. I hope the syntax is right.

enter image description here

4
  • The syntax looks correct to me. Are you entering this in an interactive Python shell, or is this saved in a file? Can you paste the unmodified script or interactive session transcript? Commented Aug 11, 2011 at 12:02
  • Is elif/else empty? If sou, add pass to it. Commented Aug 11, 2011 at 12:02
  • Can you paste the actual full output of your REPL session? It will be easier for people to see your problem if you don't replace the real output with things like "( It is giving in 'Invalid Syntax')" Commented Aug 11, 2011 at 12:03
  • Do note that guess will be an instance of the str() type and number is an instance of the int() type. When comparing the two with anything other than the equality or inequality operator may or may not be interpreted as a type-unsafe operation, based on your Python version. Commented Aug 11, 2011 at 12:12

9 Answers 9

29

It looks like you are entering a blank line after the body of the if statement. This is a cue to the interactive compiler that you are done with the block entirely, so it is not expecting any elif/else blocks. Try entering the code exactly like this, and only hit enter once after each line:

if guess == number:
    print('Congratulations! You guessed it.')
elif guess < number:
    pass # Your code here
else:
    pass # Your code here
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks! It solved my problem. :) What you'll are telling was not given in my book.
for such test, it is easier to write everything in a text file a run it with 'python mytest.py'
@jayantr7: No problem. Note that if you enter this into a file and run it through python (as Simon suggests), there won't be an issue with extra newlines; this is an issue unique to the interactive interpreter.
Thank You, I have removed the from statement before the elif Now this is working.
3

The problem is the blank line you are typing before the else or elif. Pay attention to the prompt you're given. If it is >>>, then Python is expecting the start of a new statement. If it is ..., then it's expecting you to continue a previous statement.

Comments

3

elif and else must immediately follow the end of the if block, or Python will assume that the block has closed without them.

if 1:
    pass
             <--- this line must be indented at the same level as the `pass`
else:
    pass

In your code, the interpreter finishes the if block when the indentation, so the elif and the else aren't associated with it. They are thus being understood as standalone statements, which doesn't make sense.


In general, try to follow the style guidelines, which include removing excessive whitespace.

Comments

2

In IDLE and the interactive python, you entered two consecutive CRLF which brings you out of the if statement. It's the problem of IDLE or interactive python. It will be ok when you using any kind of editor, just make sure your indentation is right.

Comments

1
 if guess == number:
     print ("Good")
 elif guess == 2:
     print ("Bad")
 else:
     print ("Also bad")

Make sure you have your identation right. The syntax is ok.

Comments

1

Remember that by default the return value from the input will be a string and not an integer. You cannot compare strings with booleans like <, >, =>, <= (unless you are comparing the length). Therefore your code should look like this:

number = 23
guess = int(input('Enter a number: '))  # The var guess will be an integer

if guess == number:
    print('Congratulations! You guessed it.')

elif guess != number:
    print('Wrong Number')

Comments

0

Besides that your indention is wrong. The code wont work. I know you are using python 3. something. I am using python 2.7.3 the code that will actually work for what you trying accomplish is this.

number = str(23)
guess =  input('Enter a number: ')
if guess == number:
   print('Congratulations! You guessed it.')
elif guess < number:
     print('Wrong Number')
elif guess < number:
     print("Wrong Number')

The only difference I would tell python that number is a string of character for the code to work. If not is going to think is a Integer. When somebody runs the code they are inputing a string not an integer. There are many ways of changing this code but this is the easy solution I wanted to provide there is another way that I cant think of without making the 23 into a string. Or you could of "23" put quotations or you could of use int() function in the input. that would transform anything they input into and integer a number.

Comments

0

Python can generate same 'invalid syntax' error even if ident for 'elif' block not matching to 'if' block ident (tabs for the first, spaces for second or vice versa).

Comments

0

indentation is important in Python. Your if else statement should be within triple arrow (>>>), In Mac python IDLE version 3.7.4 elif statement doesn't comes with correct indentation when you go on next line you have to shift left to avoid syntax error.

enter image description here

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.