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

Unlock This Lesson

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

Unlock This Lesson

Organize Your Code With Functions

00:00 Organize Your Code With Functions. So far, you’ve written your game as a script. It’s essentially a list of commands that run one after another. Now, while this is great for getting started quickly and for testing out a simple prototype of your game, these kinds of programs do not scale well.

00:18 As your program grows in complexity, you’ll want to group your code into functions that you can reuse.

00:25 At the end of this part of the course, the game will still look the same for your users, but the underlying code will be easier to extend and build out later.

00:34 You’ll start by explicitly setting up the main loop of your game. Then you’ll move the supporting code into functions. Finally, you’ll think about how you can test your game to ensure it works the way you expect. So far, you’ve set up a basic version of Wyrdl.

00:51 Think of this as a prototype where you’ve tested out some of the features that you want in the game, and you’ve got a feeling for which features are important.

00:59 You’ll now refactor your code into something that’s better prepared for your next extensions and improvements. You’ll create functions that will work as building blocks for the program.

01:10 To figure out which functions will be useful in the program, you can do a small exercise where you think of the functionality in the program top-down. At a high level, what’s the flow of the program?

01:21 Feel free to try this on your own before continuing.

01:26 On-screen, you can see one possible solution for the program flow. This shows that your game will first get a random word and then enter a loop where the user will guess words until they guess correctly or run out of guesses. Note that you don’t need to go into much detail in this chart.

01:43 For example, you don’t worry about how to get a random word or how to check a user’s guess. You only note that it should be done. The next step is to translate the flow chart into code.

01:55 Add the code seen on screen to the bottom of the Wordal PY file. Don’t delete any of your existing code yet because you’ll use it soon.

02:37 On-screen, the editor is showing that main() calls three functions that don’t exist yet: get_random_word(), show_guess(), and game_over().

02:47 You’ll create these soon, but for now, you can revel in the freedom of just imagining these building blocks are available.

02:56 You haven’t decided which parameters you’ll need to send to each supporting function. So for now, you’re using an ellipsis as a placeholder. The code inside main() is split into three sections: pre-process, process, and post-process. Once you get used to identifying the main flow of a program, you’ll note that you can often divide it up in this way.

03:19 Pre-process includes everything that needs to happen before the main loop runs, process is the job that program does during the main loop, and post-process is the work needed to clean up after the main loop.

03:32 In the Wordle clone, you pick a random word before the main loop and let the users know that the game is over after the main loop. During the main loop, you handle the user’s guesses. The main loop can end in one of two ways: either the user guesses correctly, or they make too many wrong guesses.

03:50 Unfortunately, wishful thinking isn’t enough to make main() work, so in the next part of the course, you’ll put the time in and implement the missing functions.

Become a Member to join the conversation.