Search code examples
pythonindentation

Why is python giving me an error for improper indentation when my indentation is correct?


This is the error message I got:

 File "main.py", line 15
   while True:
             ^
IndentationError: unindent does not match any outer indentation level

This is my full code:

import wikipedia
from colorama import Fore, Style, Back
y = input("tell me what you want ")
z = int(input("how many sentences "))
try:
    text = wikipedia.summary(y, sentences=z)
    print('')
    print("---Text---")
    print(text)
    print("----------")
    print(len(text.split()),"words.")   
except:
    print(Fore.RED + "ERROR)
    while True:
        print("\a")

Can you please explain why this is happening? I am using the Repl online ide.

The answer to this is that I was mixing up tabs and spaces. You shouldn't use both because this error can happen.


Solution

  • While coding in python, it's super important to pay attention to the way you indent. Of course you can either use tab or space, but always make sure you stick with one of them and not mixing them together.

    Seems like you haven't done it this time. Delete the indentations and reindent them. Should work fine.