Python Forum
Thread Rating:
  • 1 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Indentation if/else problem
#1
I need assistance with writing if/else statements on Python 3.6. I am working on an iMac OS High Sierra (v 10.13.4). This is what I write:
>>> x = 10
>>> if x < 10:
print ('less than ten')
else:

SyntaxError: inconsistent use of tabs and spaces in indentation
>>>

After the print statement my cursor returns to under the 'p' of print. In order to get the curser under the 'i' in the if statement , I have to use back arrows and forward arrows to align them. I always get the SyntaxError message. I also noticed in the tutorial that after the print statement there are three ... under the >>> prompt to write the else or elif command. I have been working on a solution for hours but cannot come up with one. If anyone can help me I would surly appreciate it. Thank you. Rikk
Reply
#2
you should always use 4 spaces per indentation level. using tab is allowed, but discouraged. In any case you should be consistent - always use only spaces or only tabs. In your case you mix the two, that's why you get that error: inconsistent use of tabs and spaces in indentation. Most editors would allow you to set an option to convert tabs to spaces automatically
the three dots ... are just to align your start position because in some of the other lines you have >>>

Finally, you are using interactive prompt. It's better to write your code as py file. In interactive mode you don't save your code for future use.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
When ever I copy and paste what is in IDLE to this forum....it does not replicate accurately.

In any event what I described in words in my first post is what is actually happening. RIKK
Reply
#4
Please, use proper tags when post code, traceback, output, etc.
See BBcode help for more info.

your problem is the mix of spaces and tabs, not wrong indentation. At least not yet...
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Mysterious Indentation Problem Dakodak 11 4,610 Jun-24-2019, 06:23 AM
Last Post: Dakodak

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020