How to select an item from the selected list?

Viewed 140

The player enters the letter (a-z). The program randomizes a word from a list starting with the entered letter and displays the drawn word. What to do to make the entered key the name of the list from which it will be possible to randomly select a word beginning with the entered character? Possibly what other way to do this? Thank you.

Code: import random

import pygame
# list of letters (a-z)
a = ["ananas", "arbuz", "agrest"]
b = ["banan", "balon", "beton"]
c = ["cytryna", "cebula", "candy"]
# ...
z = ["zebra", "zombie", "zonk"]

pygame.init()
screen = pygame.display.set_mode((0, 0))
running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

        if event.type == pygame.KEYDOWN :
            if event.key == pygame.K_ESCAPE:
                running = False
            else:
                print(event.unicode)

pygame.quit()
2 Answers

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.

Instead of creating separate lists for each character, consider using a dictionary to hold all of the lists, with the keys being the various characters. For instance,

ch_lsts = {
    "a": ["ananas", "arbuz", "agrest"],
    "b": ["banan", "balon", "beton"],
    "c": ["cytryna", "cebula", "candy"],
    ...,
    "z": ["zebra", "zombie", "zonk"]
}

Then, when you're ready to select the list of a given character, simply retrieve the specified list with ch_lsts["c"] (or whatever the character is) and do whatever you'd like to that list.

Related