Python Forum
What is causing the syntax error?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
What is causing the syntax error?
#1
Just started using python a few seconds ago...
   
    n = int(input("Enter a number: "))   

    if n % 2 == 0:
	    print (n, " is an even number")
	else:
		
SyntaxError: invalid syntax
The Else statement is highlighted but I'm not sure what's wrong.
Reply
#2
Hello and welcome to Python!
What is wrong in your case is the "empty" else.

If you don't require else clause, you don't need to write it. If you want to have the else clause, but without any action to be done, you should write "pass" keyword in it, like this:

n = int(input("Enter a number: "))   
 
if n % 2 == 0:
    print (n, " is an even number")
else:
    pass
Reply
#3
Is that your full code? I'm not getting a SyntaxError, but I am getting other errors because there is nothing after the else.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#4
(Sep-12-2017, 01:06 PM)Mr_Keystrokes Wrote: if n % 2 == 0:
print (n, " is an even number")
else:

SyntaxError: invalid syntax
[/python]


So you don't want to do anything inside else:.then just don't write else. Only if statement will do just fine.

There are some times when syntactically a statement is required but no execution is needed.
For that, you can use pass keyword.

    n = int(input("Enter a number: "))   
 
    if n % 2 == 0:
        print (n, " is an even number")
    else:
        pass
Reply
#5
As soon as I type in 'else:' and then press enter to go into the new line it pops up with invalid syntax.
In the following line I would like to write:

print (n, " is an odd number")
I am writing this in Python shell.

Strange, it seems to be working now that I pasted the full answer into the shell. Whereas before, when I tried to write the else statement it wouldn't allow me to go the next line and it highlighted the else function and indicated a syntax error.

Thank you guys for the pointers!
Reply
#6
The error could be on a line above this block of code. It's indented so I presume that this is not all the code.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Syntax error for "root = Tk()" dlwaddel 15 1,162 Jan-29-2024, 12:07 AM
Last Post: dlwaddel
Photo SYNTAX ERROR Yannko 3 374 Jan-19-2024, 01:20 PM
Last Post: rob101
  Syntax error while executing the Python code in Linux DivAsh 8 1,545 Jul-19-2023, 06:27 PM
Last Post: Lahearle
  Code is returning the incorrect values. syntax error 007sonic 6 1,206 Jun-19-2023, 03:35 AM
Last Post: 007sonic
  syntax error question - string mgallotti 5 1,297 Feb-03-2023, 05:10 PM
Last Post: mgallotti
  Syntax error? I don't see it KenHorse 4 1,243 Jan-15-2023, 07:49 PM
Last Post: Gribouillis
  Syntax error tibbj001 2 882 Dec-05-2022, 06:38 PM
Last Post: deanhystad
  Python-for-Android:p4a: syntax error in main.py while compiling apk jttolleson 2 1,830 Sep-17-2022, 04:09 AM
Last Post: jttolleson
  Mysql Syntax error in pymysql ilknurg 4 2,348 May-18-2022, 06:50 AM
Last Post: ibreeden
  Solving equation equal to zero: How to resolve the syntax error? alexfrol86 3 1,950 Feb-21-2022, 08:58 AM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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