Python Forum

Full Version: impossible to exit from the file
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi all ! This file works well, unless I try to exit. Look please at lines 17,18, 19. I am on Linux. Python 3.7.
import tty, sys, termios

class ReadChar():
    def __enter__(self):
        self.fd = sys.stdin.fileno()
        self.old_settings = termios.tcgetattr(self.fd)
        tty.setraw(sys.stdin.fileno())
        return sys.stdin.read(1)
    def __exit__(self, type, value, traceback):
        termios.tcsetattr(self.fd, termios.TCSADRAIN, self.old_settings)

def test():
    while True:
        with ReadChar() as rc:
            char = rc
        #if ord(char) == 3:
		#	sys.exit()   
        #elif ord(char) <= 32:
        if ord(char) <= 32:
            print("You entered character with ordinal {}."\
                        .format(ord(char)))
        else:
            print("You entered character '{}'."\
                        .format(char))
        #if char in "^C^D":
         #   sys.exit()

if __name__ == "__main__":
    test()
If I uncomment lines 17, 18, I have the following error:
Error:
sylvain@sylvain-HP-Laptop-15-bw0xx:~$ python get2.py File "get2.py", line 18 sys.exit() ^
Error message was incomplete:
Error:
File "get2.py", line 18 sys.exit() ^ TabError: inconsistent use of tabs and spaces in indentation
(Sep-11-2018, 09:14 AM)sylas Wrote: [ -> ]
Error:
File "get2.py", line 18 sys.exit() ^ TabError: inconsistent use of tabs and spaces in indentation

The message is clear - you mixed tabs and spaces in your code. You should never do that
@volcano It is not first time I notice interpreter tells nonsense. Please try yourself, and you will see that it is impossible to exit. Thanks for your reply.
(Sep-13-2018, 06:38 AM)sylas Wrote: [ -> ]It is not first time I notice interpreter tells nonsense.
Please, let be serious... message is clear - you mix tabs and space for indentation. fix that and it will work if there is no other problems.
(Sep-13-2018, 06:38 AM)sylas Wrote: [ -> ]@volcano It is not first time I notice interpreter tells nonsense. Please try yourself, and you will see that it is impossible to exit. Thanks for your reply.

Unclear - maybe; nonsense - in my 6 years in Python I have never noticed Doh (from interpreter Naughty )
It was introduced with Python 3.
If you are mixing tabs with white space, your program won't run.
They changed it, because before it was a bit confusing.

We don't need to try it out, because we know this. I run often into this issue until I changed my tool set.
Just use an IDE/text editor, which converts Tab-Stops into 4 white spaces.
(Sep-13-2018, 10:39 AM)DeaD_EyE Wrote: [ -> ]It was introduced with Python 3.
If you are mixing tabs with white space, your program won't run.

Good call on the part of Python development community - was not aware of that. (Well, I never use tabs)

I had my share of inherited code with a mixture of tabs and spaces - that was major PITA.
Instead of tabs I used spaces. At the second trial it works well. Surprising !!