How to use 'word generator from letters' in Python

Every line of 'word generator from letters' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
13def draw_letters():
14 """Pick NUM_LETTERS letters randomly. Hint: use stdlib random"""
15 return random.sample(POUCH, NUM_LETTERS)
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

152def get_random_word(dictionary, starting_letter=None):
153 """
154 Takes the dictionary to read from and returns a random word
155 optionally accepts a starting letter
156 """
157 if starting_letter is None:
158 starting_letter = random.choice(list(dictionary.keys()))
159
160 try:
161 to_return = random.choice(dictionary[starting_letter])
162 except KeyError:
163 msg = "Dictionary does not contain a word starting with '{}'"
164 raise NoWordForLetter(msg.format(starting_letter))
165
166 return to_return

Related snippets