Saturday, February 9, 2013

getch in python

Few days ago I started learning Python for one of the classes I'm taking. In the very first Python program I wrote, after an answer was displayed I wanted to wait for a key input from user before the next line was executed. If I was programming using C or C++ this could have been done easily using getch() function. Therefore I searched for something equivalent in Python. After reading couple of threads I realized that Python does not have something similar to getch. May be I'm wrong, there is something similar to getch and I couldn't find it. However I was able to use a simple way to produce the same effect I was looking for using the Python's input command.

input("")

Since input command prompts a massage to the user and assigns the user input to a variable, using input("") without any argument and without any variable assignment, I was able to pause the program execution until user hit 'enter'. It's not quite the same as getch but it's close. If you are looking for a getch equivalent in Python this might be useful for you.

No comments:

Post a Comment