cornsnake.util_pick

Functions for randomly selecting text. The pick_one_random function chooses a random text from a list. The pick_one_by_prompt function prompts the user to pick a text from a list.

Documentation

 1"""
 2Functions for randomly selecting text. The `pick_one_random` function chooses a random text from a list. The `pick_one_by_prompt` function prompts the user to pick a text from a list.
 3
 4[Documentation](http://docs.mrseanryan.cornsnake.s3-website-eu-west-1.amazonaws.com/cornsnake/util_pick.html)
 5"""
 6
 7import random
 8
 9
10def pick_one_random(texts: list[str]) -> str:
11    """
12    Function to pick a random text from a list.
13
14    Args:
15    texts (list): A list of texts to choose from.
16
17    Returns:
18    str: A randomly selected text from the list.
19    """
20    if not texts:
21        raise RuntimeError("Arg texts is empty")
22
23    return random.choice(texts)
24
25
26def pick_one_by_prompt(texts: list[str]) -> str:
27    """
28    Function to prompt the user to pick a text from a list.
29
30    Args:
31    texts (list): A list of texts to choose from.
32
33    Returns:
34    str: The text selected by the user.
35    """
36    if not texts:
37        raise RuntimeError("Arg texts is empty")
38
39    valid_selection = None
40    while not valid_selection:
41        print(texts)
42        selected = input("Please pick one >>")
43        if selected in texts:
44            valid_selection = selected
45    return valid_selection
def pick_one_random(texts: list[str]) -> str:
11def pick_one_random(texts: list[str]) -> str:
12    """
13    Function to pick a random text from a list.
14
15    Args:
16    texts (list): A list of texts to choose from.
17
18    Returns:
19    str: A randomly selected text from the list.
20    """
21    if not texts:
22        raise RuntimeError("Arg texts is empty")
23
24    return random.choice(texts)

Function to pick a random text from a list.

Args: texts (list): A list of texts to choose from.

Returns: str: A randomly selected text from the list.

def pick_one_by_prompt(texts: list[str]) -> str:
27def pick_one_by_prompt(texts: list[str]) -> str:
28    """
29    Function to prompt the user to pick a text from a list.
30
31    Args:
32    texts (list): A list of texts to choose from.
33
34    Returns:
35    str: The text selected by the user.
36    """
37    if not texts:
38        raise RuntimeError("Arg texts is empty")
39
40    valid_selection = None
41    while not valid_selection:
42        print(texts)
43        selected = input("Please pick one >>")
44        if selected in texts:
45            valid_selection = selected
46    return valid_selection

Function to prompt the user to pick a text from a list.

Args: texts (list): A list of texts to choose from.

Returns: str: The text selected by the user.