Intro
Now that we have covered the following in Python:
- variables;
- data types;
- Boolean logic;
- selection (IF statements);
- conditional iteration (WHILE loops);
- count-based iteration (FOR loops).
The next topic we're going to study, is procedures (usually referred to as 'functions' in Python).
Objectives
- To be able to demonstrate an understanding of procedures (AKA functions).
- To be able to demonstrate an understanding of the concept of return values.
- To be able to demonstrate an understanding of parameters (AKA arguments).
Resources
How to use functions in Python (14:54 min)
Lesson content
Number guessing game v0.1
Consider the number guessing game you wrote in an earlier lesson:
The task for this lesson, is to modify this program, so that it becomes truly procedural; to re-structure the program, so that the computational elements of the program are implemented as procedures.
Identify the distinct computational elements of the program
Essentially, this program does three things. What are they?
- Generate a random number.
- Capture input from the user.
- Guess the generated random number.
Task 1 - generating a random number
Refactor the number guessing game v0.1 program, with the generate random number functionality as a procedure (this will become version 0.2).
Task 2 - capture user guess
Refactor the number guessing game v0.2 program, with the capture user guess functionality as a procedure (this will become version 0.3).
Task 3 - guess random number
Refactor the number guessing game v0.3 program, with the guess random number functionality as a procedure (this will become version 0.4).
Ext. task 1 - evaluate guess random number
Refactor the guess_random_number() procedure from the number guessing game v0.4 program, so that the evaluate user guess functionality is abstracted into a separate procedure, that is called by the guess_random_number() procedure. This will become version 0.5.
Ext. task 2 - final output
Refactor the final output messages into a procedure. This will become version 0.6.
Ext. task 3 - validate user input
Add another procedure to validate the user's input. This will become version 0.7. It should check:
- that the value is an integer;
- that the value is between 1 - 100.
How to check if a value is an integer
There are a number of ways to do this, an efficient way is to use exception handling.
Exception handling is a mechanism to handle the exceptions that are generated by runtime errors. I have prepared a guide on how to use exception handling in Python.
Must
- Implement a program in Python that incorporates:
- defined functions;
- parameters;
- return values;
- Using the number guessing game program that you wrote last session, modify this to become procedural (all the work is done in procedures). So that there are separate functions for:
- random number generation;
- user input capture;
- guess random number.
Assessment
Submit a cropped screenshot your program, showing both the code and the program output.
Could
- Make sure your code is fully commented explaining relevant code blocks.
- Attempt some of the ext. tasks.