8

What I am trying to do is make a simple pi memorization game in Python. What I need is a way to get input from the user without having to press 'enter' after every character. It sounds like I need something like getch, but I can't get it to work. I got a getch-like function from here: https://gist.github.com/chao787/2652257#file-getch-py. I don't really understand anything that's in there. When I do 'x = getch.getch()' it says "AttributeError: '_Getch' object has no attribute 'getch'". It looks like msvcrt can do it for Windows, but I have a Mac. It also looks like curses is a thing that has getch, but it says I need to do initscr first, but then I get the error "File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/curses/__init__.py", line 30, in initscr fd=_sys.__stdout__.fileno()) _curses.error: setupterm: could not find terminal".

This is my file just using input, where you would have to press enter every time (I actually put in 1000 digits, not an ellipsis).

pi = '3.1415926535...'


def main():
    print('Welcome to PiGame!')
    pigame()
    while True:
        yn = input('Play again? y/n ')
        if yn == 'y':
            pigame()
        else: return


def pigame():

    n=0

    print('Go!')


    while n<=1000:
        x = input()
        if x == pi[n]:
            n += 1
        else:
            print('I\'m sorry. The next digit was '+pi[n]+'.')
            print('You got to '+str(n)+' digits!')
            return
    print('You got to 1000! Hooray!')
5
  • 1
    see stackoverflow.com/questions/510357/… Commented Jan 3, 2015 at 0:43
  • Yes, I read that one. That's the same code that gives me the attribute error. Commented Jan 3, 2015 at 2:20
  • Should I have added a response or a comment on that thread saying it didn't work? Commented Jan 3, 2015 at 3:36
  • Such a comment (definitely not an answer!) will full details about what you've tried and your environment (e.g by a pointer to this Q) might help others, so it's far from a bad idea. Commented Jan 3, 2015 at 3:42
  • Does this answer your question? How to read a single character from the user? Commented Jul 29, 2021 at 15:20

2 Answers 2

12

You can define your own version of getch using the termios, sys and tty packages:

def getch():
    import termios
    import sys, tty
    def _getch():
        fd = sys.stdin.fileno()
        old_settings = termios.tcgetattr(fd)
        try:
            tty.setraw(fd)
            ch = sys.stdin.read(1)
        finally:
            termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
        return ch
    return _getch()
Sign up to request clarification or add additional context in comments.

3 Comments

termios.error: (25, 'Inappropriate ioctl for device')
needs to be return _getch() in the end (you forgot parenthesis)
Apparently termios does not exist on Windows system. Is there any way to adapt Term::ReadKey from perl which does work on Windows?
1

This is a tested (on RPi, Py 3) code that can read a specified length of chars without need to hit Enter button

But consider one thing :

This must run on terminal otherwise raises an error

import termios, sys , tty
def _getch():
   fd = sys.stdin.fileno()
   old_settings = termios.tcgetattr(fd)
   try:
      tty.setraw(fd)
      ch = sys.stdin.read(1)     #This number represents the length
   finally:
      termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
   return ch
getch = _getch()
print(getch)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.