elif statement indentation syntax error with Python IDLE v3.5 on Windows
Clash Royale CLAN TAG#URR8PPP
elif statement indentation syntax error with Python IDLE v3.5 on Windows
Why the elif always failed with syntax error here?
I used Python IDLE 3.7.0 for windows 64bits.
It works well with baisc strings, numbers, and lists.
But when I tried if - elif statements in 3 ways, all failed with syntax error.
Could anybody help?
The code I used was from the python documents chapter 4.1 if statements as shown below.
Copy and paste the original from the python documents.
But if failed with syntaxerror: unindent does not match any outer indentation level.
Write print statement in the same line as elif, but it still failed with syntax error:
Indent the elif with the same place as if. Still failed.
For images with text, it is much more helpful and appreciated if you enter the code as text into the question as you did with the rest of the question text. copy/paste code, select the code block, then press
button and it will format, thanks.– davedwards
Aug 10 at 22:35
For complicated statements like this, it would be better to paste into the editor where there is not prompt to confuse indentation. I have a tem.py and tem2.py in my 'playaround' directory. I use it often enough that it is usually in the recent files list, hence easy to access.
– Terry Jan Reedy
Aug 13 at 6:13
Thank you guys. I understand it would be confused to display pictures here. I would upload the code in the future and use the .py file to execute instead of IDLE directly.
– Tiffany Wang
Aug 15 at 17:43
1 Answer
1
The IDLE prompt is weird and confusing. It prints >>>
in front of the first line of a multi-line statement, but unlike regular interactive Python, it doesn't print ...
in front of continuation lines. The >>>
in front of the first line doesn't count towards its indentation, so what looks to you like
>>>
...
>>>
>>> if x < 0:
...
elif x == 0:
...
looks to IDLE like
if x < 0:
...
elif x == 0:
...
The best solution is to stop using IDLE. If you want to use IDLE anyway, then pretend the >>>
isn't there when indenting:
>>>
>>> if x < 0:
...
elif x == 0:
...
Thanks! I know two things: 1. use .py documents instead of IDLE directly. 2. type code directly here instead of pictures.
– Tiffany Wang
Aug 15 at 17:42
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
number 1's almost right, but your elifs are one space too far to the right - they should line up with the if. btw it's MUCH easier to answer the qu if you type your code here
– Joel Berkeley
Aug 10 at 22:00