Python Forum
Why interpreter accepts indentation made with spaces and not those by tabs - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Why interpreter accepts indentation made with spaces and not those by tabs (/thread-13484.html)

Pages: 1 2


Why interpreter accepts indentation made with spaces and not those by tabs - sylas - Oct-17-2018

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.


RE: Why interpreter accepts indentation made with spaces and not those by tabs - buran - Oct-17-2018

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.


RE: Why interpreter accepts indentation made with spaces and not those by tabs - sylas - Oct-17-2018

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 ???


RE: Why interpreter accepts indentation made with spaces and not those by tabs - sylas - Oct-18-2018

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 ??


RE: Why interpreter accepts indentation made with spaces and not those by tabs - buran - Oct-18-2018

https://stackoverflow.com/a/1024489/4046632


RE: Why interpreter accepts indentation made with spaces and not those by tabs - Larz60+ - Oct-18-2018

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.


RE: Why interpreter accepts indentation made with spaces and not those by tabs - sylas - Oct-18-2018

I tried again because of you. My way works very well. Please try it yourself and then reply.


RE: Why interpreter accepts indentation made with spaces and not those by tabs - buran - Oct-18-2018

(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...


RE: Why interpreter accepts indentation made with spaces and not those by tabs - sylas - Oct-18-2018

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.


RE: Why interpreter accepts indentation made with spaces and not those by tabs - buran - Oct-18-2018

for the benefit of others, who may find this thread in the future.
https://howchoo.com/g/m2u0nthkyti/vim-convert-tabs-to-spaces