I start learning about python, so i read the tutorial:
they show this:

>>> x = int(raw_input("Please enter an integer: "))
Please enter an integer: 42
>>> if x < 0:
... x = 0
... print 'Negative changed to zero'
... elif x == 0:
... print 'Zero'
... elif x == 1:
... print 'Single'
... else:
... print 'More'
...
but i dont know how to get the elif indent!!!
can anyone help me? I use python 2.6 GUI.
bissically when I press the tab button, it indents to the print statement, but not where I can put elif. TY

Dani AI

Generated

the issue is the interactive shell keeps you inside the indented block until you move the cursor back to the left margin. is right: press Backspace until the caret is at column 0, then type elif. Also, when you finish the whole if/elif/else statement, hit Enter on a completely blank ... line to execute it.

Quick walkthrough in the shell:

>>> n = 7
>>> if n < 0:
...     print 'neg'
... # Backspace to the left margin here
... elif n < 10:
...     print 'one digit'
... else:
...     print 'two+ digits'
...     # Press Enter on a blank line to finish
...

is spot on about using 4 spaces. In practice, never mix tabs and spaces; that is what triggers those confusing IndentationError/TabError messages. Configure your editor so the Tab key inserts 4 spaces.

And +1 to ’s point: avoid WordPad for code. Use an editor with Python settings (IDLE’s Editor, Notepad++, VS Code, etc.). Turn on these options if your editor has them:

  • Insert spaces when Tab is pressed.
  • Auto-indent new lines.
  • Show whitespace so tabs vs. spaces are visible.
  • Convert tabs to spaces on save.

Tip for future readers: in Python 3, raw_input became input, but the indentation rules and the shell behavior above are the same.

Recommended Answers

All 5 Replies

u need to press backspace button before entering elif. In this case carret will be put at the beginning of python shell and you can type elif.

Just press space 4 times, then you have it indented the right amount. Do this for all of them, not the actual if statement but the stuff afterwards.
So

>>>if stuff == True:
...     print "stuff" #i pressed space 4 times
... else:
...     print 'no'

My advice, don't use the Python Shell for programming, use an editor. The Python Shell interative interpreter is only for very short tests. It is basically there to confuse beginners and discourage them from using Python. :)

yeah i m using wordpad now.

If you are using windows, I would recommend notepad++

You can set the language so it will highlight your code for you.
I believe you can also set it to run the script immediately from the editor without too many headaches:

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.