Can't seem to get .getch() to work (Python 2.7)

833 views Asked by At

I'm trying to detect a key press to determine whether the user wants to play again, but msvcrt.getch() is just not working for me. This is my code:

import msvcrt
#the game here
    print "Do you want to continue? Y/N"
    if msvcrt.getch() == 'Y' or msvcrt.getch() == 'y':
        print"Let's play again!"
        print"-----------------"
    elif msvcrt.getch() == 'N' or msvcrt.getch() == 'n' :
        "Okay, hope you had fun"
        break

Any suggestions?

EDIT: The answers below do work on the command line, for some reason just don't in PyCharm

1

There are 1 answers

1
AudioBubble On BEST ANSWER

You should only call msvcrt.getch() once. Change your code to something like this:

import msvcrt
#the game here
    print "Do you want to continue? Y/N"
    response = msvcrt.getch()
    if response.lower() == 'y':
        print"Let's play again!"
        print"-----------------"
    elif response.lower == 'n' :
        "Okay, hope you had fun"
        break