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

  • Context: Python 
  • Thread starter Thread starter clope023
  • Start date Start date
  • Tags Tags
    Error Python
Click For Summary
SUMMARY

The discussion addresses syntax errors encountered in Python version 2.7.1 when using the else statement. The user reports persistent syntax errors despite following examples from tutorials. Key insights include the necessity for proper indentation of the else statement at the same level as the if statement, and ensuring that the else suite is on a separate line. Correct syntax is demonstrated with the example: if (x/2)*2 == x: followed by an indented print 'Even' and else: with an indented print 'Odd'.

PREREQUISITES
  • Understanding of Python 2.7.1 syntax
  • Knowledge of control flow statements in Python
  • Familiarity with indentation rules in Python
  • Basic programming concepts such as variables and conditional statements
NEXT STEPS
  • Review Python 2.7.1 documentation on control flow statements
  • Practice Python indentation rules with various examples
  • Explore differences between Python 2.x and Python 3.x syntax
  • Learn about debugging techniques for Python syntax errors
USEFUL FOR

Beginner Python programmers, educators teaching Python syntax, and anyone troubleshooting syntax errors in Python 2.7.1.

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!
 

Similar threads

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