Python Forum
Why interpreter accepts indentation made with spaces and not those by tabs
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Why interpreter accepts indentation made with spaces and not those by tabs
#1
Hi all! It is not agreable to rewrite a file where indentation was made with tabs. I met this problem on geany and also with vim.
Reply
#2
You cannot mix tabs and spaces. You need to select one and be consistent in using it. Recommendation is to use 4 spaces. Also you may configure your IDE to convert 1 tab to 4 spaces. Note that if you have tab indented file and open it in IDE configured to convert tabs, it will not convert existing tabs and you will end with mix of tabs and spaces. Also there might be setting to detect and use whatever indentation is used.
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
For the whole file I used tabs. I used vim. It did not run well.
sylvain@sylvain-HP-Laptop-15-bw0xx:~$ cat new2.py
#!/usr/bin/python3
#!/usr/bin/env python3
#new2.py

def silly(n, x):
    m=n
    stack = [iter(range(m))]
    total=0
    while stack:
        try:
            i = stack[-1].__next__()
        except StopIteration:
            stack.pop()
            continue
        total += i   
        if total >= x:
            break
        if i ==  5:
            m -= 1
            stack.append(iter(range(m)))
    return total


print(silly(1,2))


print(silly(0,1))
print(silly(2,3))
print(silly(3,4))
										
sylvain@sylvain-HP-Laptop-15-bw0xx:~$ 
The first is for try: Instead of tabs I use spaces. Error repaired. Next error is for the next line.nd
It is really amazing ! How interpreter finds you used tabs and not spaces ???
Reply
#4
I think that if you have a file like the one above, a good way is to use tabs, once finished your file you copy it, you delete your original file, and at last you paste. Isn't it ??
Reply
#5
https://stackoverflow.com/a/1024489/4046632
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
#6
You don't read posts do you.
Quote:Recommendation is to use 4 spaces
Buran explained why you should not use tabs, but yet you ignore his advice.
Reply
#7
I tried again because of you. My way works very well. Please try it yourself and then reply.
Reply
#8
(Oct-18-2018, 06:12 PM)sylas Wrote: I tried again because of you. My way works very well. Please try it yourself and then reply.
Wall Wall Wall Wall
I have no intonation to try... do whatever you want, but please show mercy and don't share it with us...
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
#9
The way I give you, using tabs, wins a lot of time. I think many people are interested by that. Maybe someones use it already. I repeat I use Vim (the white one for windows)
. Vim is for me the best tool.
Reply
#10
for the benefit of others, who may find this thread in the future.
https://howchoo.com/g/m2u0nthkyti/vim-co...-to-spaces
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
  cmath.rect accepts a negative modulus JMB 2 328 Jan-17-2024, 08:00 PM
Last Post: JMB
  function accepts infinite parameters and returns a graph with those values edencthompson 0 855 Jun-10-2022, 03:42 PM
Last Post: edencthompson
  Buttons not working in tabs kenwatts275 2 1,365 May-02-2022, 04:45 PM
Last Post: kenwatts275
  PYQT charts in tabs frohr 10 4,380 Feb-13-2022, 04:31 PM
Last Post: Axel_Erfurt
  create new tabs and populate using python reggie4 2 2,257 Jan-23-2021, 11:25 PM
Last Post: Larz60+
  HELP TabError: inconsistent use of tabs and spaces in indentation blackjesus24 2 3,545 Jan-30-2020, 10:25 AM
Last Post: blackjesus24
  Setting Tabs in a listbox scratchmyhead 2 2,801 Nov-12-2019, 11:18 PM
Last Post: scratchmyhead
  removing spaces/tabs after used .strip() zarize 0 1,580 Sep-11-2019, 12:46 PM
Last Post: zarize
  removing tabs from input text jenya56 3 3,218 Mar-28-2019, 03:10 PM
Last Post: DeaD_EyE
  Writing a function that accepts a path to an input file says 3 3,283 Aug-29-2018, 05:20 AM
Last Post: buran

Forum Jump:

User Panel Messages

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