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

  • Thread starter Thread starter clope023
  • Start date Start date
  • Tags Tags
    Error Python
AI Thread 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!
 
Thread 'Star maps using Blender'
Blender just recently dropped a new version, 4.5(with 5.0 on the horizon), and within it was a new feature for which I immediately thought of a use for. The new feature was a .csv importer for Geometry nodes. Geometry nodes are a method of modelling that uses a node tree to create 3D models which offers more flexibility than straight modeling does. The .csv importer node allows you to bring in a .csv file and use the data in it to control aspects of your model. So for example, if you...
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...
I am trying to run an .ipynb file and have installed Miniconda as well as created an environment as such -conda create -n <env_name> python=3.7 ipykernel jupyter I am assuming this is successful as I can activate this environment via the anaconda prompt and following command -conda activate <env_name> Then I downloaded and installed VS code and I am trying to edit an .ipynb file. I want to select a kernel, via VS Code but when I press the button on the upper right corner I am greeted...
Back
Top