Python Forum
impossible to exit from the file
Thread Rating:
  • 1 Vote(s) - 2 Average
  • 1
  • 2
  • 3
  • 4
  • 5
impossible to exit from the file
#1
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() ^
Reply
#2
Error message was incomplete:
Error:
File "get2.py", line 18 sys.exit() ^ TabError: inconsistent use of tabs and spaces in indentation
Reply
#3
(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
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply
#4
@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.
Reply
#5
(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.
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
(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 )
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply
#7
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.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#8
(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.
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply
#9
Instead of tabs I used spaces. At the second trial it works well. Surprising !!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Is changing processes by using match with type function impossible? cametan 1 136 May-30-2024, 02:16 PM
Last Post: cametan
  python difference between sys.exit and exit() mg24 1 1,966 Nov-12-2022, 01:37 PM
Last Post: deanhystad
  Impossible to cleanly uninstall Python 3.8.9 Ftaniere 2 1,817 Sep-01-2022, 10:38 AM
Last Post: snippsat
  Failing reading a file and cannot exit it... tester_V 8 1,912 Aug-19-2022, 10:27 PM
Last Post: tester_V
  Even if Atom is on the whisker menu, impossible to run it sylas 9 4,443 Jun-21-2018, 07:32 AM
Last Post: sylas

Forum Jump:

User Panel Messages

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