Step 5: Define get_random_word function
- This function takes a list
wordsas an argument. - The function picks a random position (index) in the list.
- Returns the word from the
wordslist at that position.
๐ก Hint
- Use
randrange(...)orrandint(...)from therandommodule to generate a random number that can be used as an index when accessing an item in a list. Store this number in a variable.
โน๏ธ More Information
.randrange(start, stop)or.randrange(stop)returns a random integer starting fromstart(or 0 by default) up to but not includingstop.
.randint(a, b)returns a random integer betweenaandb, including both values.
- Make sure the index is within the valid range of the
wordslist. The valid index range is from0tolen(words) - 1, wherelen(words)returns the number of items in the listwords. - To call a function from a module:
moduleName.functionName(...) - To return a word from the list of words, think about how to access an item from a list.
โญ Example
word_list[index]returns the item at the specified index in the list.
โ Code Runner
Define get_random_word function
Type or paste your code and run.