54.9k views
3 votes
This function should pick a word from a list of all lowercase options, and return the picked word. It must be called get_word. It should return the word that gets selected. This is the code we have so far, but there are 3 errors that need to be fixed before it will work correctly.

import random
def getwrd()->str:
all_words =
['above','blame','corny','dress','EMAIL','flail','glass','horse','igloo','judge','kebab','lilac','moose','never','olive','prize','query','react','shrub','toxic','untie','vigor','wordl','yield','zesty']
my_word = random.choice(all_words)
return 'hello'
Right now our word list is small, but we could grow this by adding more items to the list, importing a very large list from a file, or randomly generating 5 letters. Running the following code should print out the word 'email': random.seed(1) print(get_word())

User Mixchange
by
8.0k points

1 Answer

5 votes

Final answer:

The provided code snippet should be corrected to have a consistent function name, return the chosen word, and ensure that all words in the list are lowercase to function properly.

Step-by-step explanation:

The code provided has a function that is supposed to return a randomly selected word from a given list of words, but there are three errors that we need to correct:

  1. The function name in the definition is getwrd but it's called as get_word. We need to ensure that the function definition and the function call have the same name.
  2. The return statement should return the variable my_word instead of the string 'hello'.
  3. One word in the list, 'EMAIL', is in uppercase. Since we want all lowercase options, we should make this word lowercase.

Here is the corrected code:

import random

def get_word()->str:
all_words = ['above','blame','corny','dress','email','flail','glass','horse','igloo','judge','kebab','lilac','moose','never','olive','prize','query','react','shrub','toxic','untie','vigor','wordl','yield','zesty']
my_word = random.choice(all_words)
return my_word

After applying these corrections, the function should work correctly as intended. Additionally, if you want to ensure that the word "email" is chosen every time for testing purposes, you can set the random seed just before calling the function as shown:

random.seed(1)
print(get_word())

User Yazid
by
8.4k points