I implemented the following function using a dictionary though it is not necessary. Depending on what you would like to do with the word list, you can write the script with or without using the dictionary. (Both of which should be easy to directly implement into your pygame script)
import keyboard
import random
a = ["ananas", "arbuz", "agrest"]
b = ["banan", "balon", "beton"]
c = ["cytryna", "cebula", "candy"]
# ...
words_dict = {}
words_dict['a'] = a
words_dict['b'] = b
words_dict['c'] = c
# ...
while True:
try:
if keyboard.is_pressed('a'):
print(random.choice(words_dict['a']))
break
elif keyboard.is_pressed('b'):
print(random.choice(words_dict['b']))
break
elif keyboard.is_pressed('c'):
print(random.choice(words_dict['c']))
break
# implement more elif here
except:
break
Instead of assigning each list with a key into the dict like what I did above, you can also directly create a dict with everything included:
words_dict = {
'a': ["ananas", "arbuz", "agrest"],
'b': ["banan", "balon", "beton"],
# and so on
}
Without using a dictionary:
import keyboard
import random
a = ["ananas", "arbuz", "agrest"]
b = ["banan", "balon", "beton"]
c = ["cytryna", "cebula", "candy"]
# ...
while True:
try:
if keyboard.is_pressed('a'):
print(random.choice(a))
break
elif keyboard.is_pressed('b'):
print(random.choice(b))
break
elif keyboard.is_pressed('c'):
print(random.choice(c))
break
# implement more elif here
except:
break
============================================================
Update: The following script detects the user's key clicks and retrieves/displays the correct information without having to use cases to check which key the user entered.
import random
import msvcrt
words_dict = {
'a': ["ananas", "arbuz", "agrest"],
'b': ["banan", "balon", "beton"],
'c': ["cytryna", "cebula", "candy"]
# ...
}
while True:
if msvcrt.kbhit():
key_pressed = msvcrt.getch()
try:
if words_dict.__contains__(key_pressed.decode('utf-8')):
print(random.choice(words_dict[key_pressed.decode('utf-8')]))
break
else:
print(f'Input "{key_pressed.decode("utf-8")}" is not recognized. Please try again!')
except:
break
Note that the variable key_pressed is of type bytes and needs to be decoded like above before been used.