r/pythonhelp • u/Auklin • Nov 03 '22
Can't enter this SIMPLE case in a match statement
def on_press(key):
print(type(key))
x = str(key)
print (x, type(x))
match x:
case 'w':
print("stop")
etc...Output after pressing the 'w' key:
<class 'pynput.keyboard._win32.KeyCode'>
'w' <class 'str'>No "stop" is printed, so it fails to enter the match case.
3
Upvotes
1
u/AmongstYou666 Nov 03 '22
# if python version < 3.10def do1():
print("one stuff")def do2():
print("two things")Case = {1: 'do1()', 2: 'do2()'}inp = int(input("Choose 1 or 2: "))try:
eval(Case[inp])except KeyError:
print("Not a valid choice")
1
u/Goobyalus Nov 03 '22
You're matching the
_win32KeyCodeobject against a string. Convert to string, then match.