I'm new to python and am having trouble with the getch.getch command. I am creating a program that will print a binary numpy array that the user inputted - without hitting enter. To do this, I created an array of zeros and a list of letters with the coordinates that should be converted to ones, e.g.: a = ([x1, x2, x3],[y1, y2, y3]). I had the program working fine with a simple input() command. But when I switched to getch.getch, it prompted the following index error: "only integers, slicers ..." Is my program unable to recognize a getch input as a defined variable? What can I do to fix this problem? Thanks!
Python: Getch() not Recognized as a Defined Variable
588 views Asked by Mark Sperling At
1
I'm guessing your code looks something like this:
And the result usually looks like this:
And now you want to use
getch
instead ofinput
. Am I on the right track?in Python 2.7,
getch
andinput
work quite differently.input
gets the user's input, evaluates it as if it were an expression, and returns the result.getch
merely returns the character the user typed. This is whyinput
gives you(4,8,15)
andgetch
gives you"a"
.Instead of storing each value as a separate variable, store all of them in a single dict, using their old variable names as keys. Then you can access the value if you know its name.