Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Creating Supporting Functions

00:00 Create Supporting Functions. Currently, the main() function won’t run. You haven’t implemented get_random_word(), show_guess(), or game_over().

00:11 That’s a bad situation because if you can’t run your function, then you can’t test it to make sure that it does what you expect it to do. Now you’ll implement these three functions, mostly by moving existing code that you wrote earlier on. Start by considering get_random_word().

00:28 What should the function do? You can use these requirements as a guide when implementing it: choose a random word from an existing word list and ensure that the word is five letters long. When implementing a new function, an important decision is which parameters the function should accept.

00:45 In this case, you could pass in a word list or a path to a word list. However, to keep things simple, you’ll hard-code the path to the word list within the function.

00:55 This means that you won’t need any parameters. Add the code seen on-screen. Note that you’ve already written most of the code that ends up inside get_random_word().

01:07 You can move the lines from your earlier implementation into this function. First, the WORDLIST constant is removed.

01:18 Next, the original code block is cut from the top of the file, and then at the bottom of the file, the new function is defined with a wordlist variable now reading the words from the file.

01:38 The original code is now pasted back in and indented appropriately. There are a couple of changes to make to the code. Firstly, changing the reference to the WORDLIST constant to the new wordlist variable,

01:55 and secondly, returning the random choice rather than assigning it to the variable word. As earlier, you read a word list from a file and then filter the list so that you are left with words at the correct length.

02:08 Reading the word list each time you get a new word could potentially be slow. However, in this game, you are only calling get_random_word() once, so this will not be an issue.

02:19 The next function you need to implement is show_guess(). Move the code into the following function, firstly by finding it in the original code, selecting it, and cutting it. Then move to the bottom of the file, start the function definition, and paste the code in, making sure it’s indented appropriately.

02:45 This new function first categorizes the letters in the user’s guess into correct, misplaced, and wrong letters as earlier. Then these are printed to the console. The code is the same as before, just reimplemented as a function.

03:01 The last function that you’ll implement for now is game_over(). At the moment, it might be overkill to refactor this into a separate function, as it will only print a message to the screen. But by dividing your code like this, you are naming that particular part of the code, clearly indicating what it’s doing. You can also expand on this later on if needed.

03:22 Find the original code in the file and delete it.

03:29 Then at the bottom, add the new code as seen on-screen. The function accepts word as a parameter and notifies the user by printing it to the terminal with an f-string.

03:42 You are now ready to do the final tweaks on main(), which you set up earlier. In particular, you need to fill in the ellipses that you used as placeholders and call main() to start the game.

03:53 The first thing to do is to remove the last piece of the original code and then add the necessary arguments to each function call.

04:15 To finish up the refactoring, you call main() using the name-main idiom. You do this by adding the code seen on-screen at the end of the source file.

04:28 These lines make sure the code is called when the file is executed. You’ve made changes all over your file in this step, but the game should be back in a playable state.

04:39 Try running it to see if it functions correctly.

04:51 In the next section of the course, you’ll look at how to test your code to ensure that it works as it should.

Become a Member to join the conversation.