TabError: inconsistent use of tabs and spaces in indentation in Python

Dung Do Tien Oct 18 2021 129

Hello you guys, I a newbie in Python and I'm also studying more about Python.

I feel to dificult to learn Python because I got many exception format tab/space syntax. I have some line of code as below:

even = 0
odd = 0
count = 5
for i in range(count):
    num = float(input("Type a number:"))
    if num % 2 == 0:
        even +=1  
    else:
        odd +=1
        
print(even)
print(odd)

But I get an exception throw TabError: inconsistent use of tabs and spaces in indentation when I run code above.

I tried remove all space and using tab to align syntax but it not solve for me.

File "main.py", line 6
    if num % 2 == 0:
                   ^
TabError: inconsistent use of tabs and spaces in indentation

And I am using python 3.x

Anyone can explain it to me? 

How can I solve it?

Thanks for any suggestions.

Have 3 answer(s) found.
  • Q

    Quoc Tin Nguyen Oct 18 2021

    Please dont' use TAB to make code with Python. You can set indent in your IDE.

    1. Set your editor to use 4 spaces for indentation.
    2. Make a search and replace to replace all tabs with 4 spaces.
    3. Make sure your editor is set to display tabs as 8 spaces.

     I usually set indent default and with I press ENTER editor auto align for me.

  • X

    Xhana Ahmetaj Oct 18 2021

    I suggest using autopep8 plugin to help auto format code and help you code Python easy!! Run comand below to install plugin:

    $ pip install --upgrade autopep8

    To modify a file in place, run command: 

    $ autopep8 --in-place --aggressive --aggressive <filename>

    You can see it will auto format syntax for you.

  • N

    Nguyễn Phong Oct 18 2021

    Please using Enter and Tab line by line, See code format below:

    even = 0
    odd = 0
    count = 5
    for i in range(count):
        num = float(input("Type a number:"))
        if num % 2 == 0:
            even +=1  
        else:
            odd +=1
            
    print("even: ", even)
    print("odd: ",odd)

    I'm not use SPACE and below is #output:

    Type a number:1
    Type a number:2
    Type a number:3
    Type a number:4
    Type a number:5
    even:  2
    odd:  3

    But Ithink you should config tab indent in your IDE.

Leave An Answer
* NOTE: You need Login before leave an answer

* Type maximum 2000 characters.

* All comments have to wait approved before display.

* Please polite comment and respect questions and answers of others.

Popular Tips

X Close