Python 3: Guess the number –The return

This entry is part 12 of 14 in the series Python Tutorial

We have seen this example previously. There I included a lot of loops to handle user input and the main logic… Now it is time to refactor the application to use functions. This should make the code readable and we could re-use parts later.

One of those re-usable parts is the number reading. We have two points where we want the user to enter a number and have written slightly the same code. This is a very good place to start refactoring and extract this into one function.

So let’s extract this into a function. The only difference is the message we print to ask the user for a number. This can be handled as a parameter of the function because this is the only variable part. So let’s see the definition of the function:

As you can see this is a very straightforward approach where we ask the user for input and return it if it is a number.

Naturally we cannot handle the verification of the number to be 100 or 1000 in this method so we have to modify out code block there too.

At the end of this simple modification we have the application with following code (and it has the same functionality):

This looks like a very nice code without duplication and it is very lean. However if you know something about testing you might say that this code has a very big “main” block which is hard to test.

I have to agree. Unit testing (although I will write about it in a later chapter) would be bothersome here. The solution would be to split this big main loop into smaller functions which can be tested on their own.

For example evaluating if the user has won or lost. For this we could write a function which takes the secret number, the guesses, the guess count and the guessed number as input and would evaluate the message to tell the user. But four parameters for a function are quite much so let’s divide it for now. I’ll create a function which tells the user if the secret number is higher or lower as the guessed number — based on these inputs.

In the function user_won we use a return statement to indicate if the user has won. If not then we return the implicit None which will evaluate to false.

One more thing we can test is asking the user if he/she wants to play another round or not.

After all these changes let me show you again the full code:

 

Series Navigation<< Python functionObject-Oriented Programming (OOP) >>

Leave A Comment

Your email address will not be published. Required fields are marked *