# Python language basics 22: organising code into functions

Introduction

In the previous post we looked at functions which are one of the most important building blocks of any serious programming language. We saw how to use the ‘def’ keyword to define a function. We also discussed the difference between void functions – methods – and functions that have a return value.

In this post we’ll organise our previous number guessing game into functions as an exercise.

Note that there’s most often no single solution when you break up your code into functions. The one presented here is only one solution out of many.

Starting point

As a reminder here’s the number guessing game code:

```from random import randint

lowerLimitInput = input('Lower limit: ')
upperLimitInput = input('Upper limit: ')

randomNumber = randint(int(lowerLimitInput), int(upperLimitInput))

guess = int(input('Your first guess: '))
guessCounter = 1

while randomNumber != guess:
if guess > randomNumber:
print('Too large')
else:
print('Too small')
guessCounter += 1
guess = int(input('Your next guess: '))

print('Congratulations. It took you ' + str(guessCounter) + ' guesses to guess the correct number.')
```

Let’s go step by step. We ask the use to enter an integer a number of times. Let’s put that into a get_integer_input function and use it in the code:

```from random import randint

def get_integer_input(prompt_text):
user_input = input(prompt_text)
return int(user_input)

lowerLimitInput = get_integer_input('Lower limit: ')
upperLimitInput = get_integer_input('Upper limit: ')

randomNumber = randint(lowerLimitInput, upperLimitInput)

guess = get_integer_input('Your first guess: ')
guessCounter = 1

while randomNumber != guess:
if guess > randomNumber:
print('Too large')
else:
print('Too small')
guessCounter += 1
guess = get_integer_input('Your next guess: ')

print('Congratulations. It took you ' + str(guessCounter) + ' guesses to guess the correct number.')
```

Next we’ll put the evaluation of the guess into its own function:

```from random import randint

def get_integer_input(prompt_text):
user_input = input(prompt_text)
return int(user_input)

def evaluate_guess(guess, random_number):
if guess > random_number:
print('Too large')
else:
print('Too small')

lowerLimitInput = get_integer_input('Lower limit: ')
upperLimitInput = get_integer_input('Upper limit: ')

randomNumber = randint(lowerLimitInput, upperLimitInput)

guess = get_integer_input('Your first guess: ')
guessCounter = 1

while randomNumber != guess:
evaluate_guess(guess, randomNumber)
guessCounter += 1
guess = get_integer_input('Your next guess: ')

print('Congratulations. It took you ' + str(guessCounter) + ' guesses to guess the correct number.')
```

We then create separate functions for different stages in the game. You’ll notice that we rename the camel case variables to adhere to the lower case character convention in Python:

```from random import randint

def get_integer_input(prompt_text):
user_input = input(prompt_text)
return int(user_input)

def evaluate_guess(guess, random_number):
if guess > random_number:
print('Too large')
else:
print('Too small')

def finish_game(guess_counter):
print('Congratulations. It took you ' + str(guess_counter) + ' guesses to guess the correct number.')

def play_game(lower_limit, upper_limit):
random_number = randint(lower_limit, upper_limit)

guess = get_integer_input('Your first guess: ')
guess_counter = 1

while random_number != guess:
evaluate_guess(guess, random_number)
guess_counter += 1
guess = get_integer_input('Your next guess: ')

return guess_counter

def start_game():
lower_limit_input = get_integer_input('Lower limit: ')
upper_limit_input = get_integer_input('Upper limit: ')
guess_counter = play_game(lower_limit_input, upper_limit_input)
finish_game(guess_counter)
```

We can probably stop there.

We saw in this post how to execute the original form of the number guessing game from the command line. Try to do it now. You’ll see that the program simply exists without starting the game.

That’s expected as all we have now is a collection of functions. Python will compile them but won’t automatically know that you want to call one of them. How would it know? How could it possibly know where to start?

We’ll see how to resolve this situation in the next post.

Read all Python-related posts on this blog here. About Andras Nemes
I'm a .NET/Java developer living and working in Stockholm, Sweden.

Elliot Balynn's Blog

A directory of wonderful thoughts

Software Engineering

Web development

Disparate Opinions

Various tidbits

chsakell's Blog

WEB APPLICATION DEVELOPMENT TUTORIALS WITH OPEN-SOURCE PROJECTS

Once Upon a Camayoc

Bite-size insight on Cyber Security for the not too technical.