Python Python Else/Elif Syntax Error: Troubleshooting Tips and Common Mistakes

  • Thread starter Thread starter clope023
  • Start date Start date
  • Tags Tags
    Error Python
Click For Summary
The discussion revolves around a user experiencing syntax errors when using the else statement in Python 2.7.1. The user provided an example program intended to check if a number is even or odd but encountered issues specifically with the else statement. Despite following tutorials, the user received syntax errors, leading to confusion about whether Python 2.7.1 recognizes else statements or if the syntax has changed. Other participants clarified that the else statement must be indented at the same level as the if statement and placed on a separate line. Proper indentation is crucial in Python, and the correct structure was provided to resolve the syntax error.
clope023
Messages
990
Reaction score
130
Hello, the title says it all; I am trying to teach myself Python using version 2.7.1 aka matplotlib or PYLAB via the MIT opencourseware as well as the tutorial from the site I downloaded it from; I am having a very frustrating time using it as system continues to return any variant of else statements as syntax errors.

An attempted example program is as follows:

x = 2

if (x/2)*2 == x:
... print 'Even'
... else: print 'Odd'
...
...
'Even'

This would be my expected output but, python continues to give me a syntax error whenever I use the else statement. I've tried it without the colons, I've tried added the else's command on another line, I've tried using the elif function and nothing works the editor contiues to give syntax errors.

I know I'm using the correct code as I am pulling several examples from the tutorial and they've all been correct except this one. Does python 2.7.1 not recognize else statements? Does anyone know if the syntax has changed? Does anyone know the correct syntax? Any and all help is greatly appreciated.
 
Last edited:
Technology news on Phys.org
The else statement needs to be indented at the same level as the if, the else suite needs to be on a separate line (or lines), and this suite needs to be indented at the same at the same level as the if suite. e.g.

Code:
...
if (x/2)*2 == x:
   print 'Even'
else:
   print 'Odd'
...
 
D H said:
The else statement needs to be indented at the same level as the if, the else suite needs to be on a separate line (or lines), and this suite needs to be indented at the same at the same level as the if suite. e.g.

Code:
...
if (x/2)*2 == x:
   print 'Even'
else:
   print 'Odd'
...

Thanks very much!
 
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 10 ·
Replies
10
Views
3K
Replies
6
Views
3K
  • · Replies 18 ·
Replies
18
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
1
Views
5K
  • · Replies 43 ·
2
Replies
43
Views
4K
  • · Replies 1 ·
Replies
1
Views
2K