Challenges 118 - 123: Subprograms 99
Challenges 118 - 123
Subprograms
Explanation
Subprograms are blocks of code which perform specific
tasks and can be called upon at any time in the program to
run that code.
Advantages
! You can write a block of code and it can be used and re-
used at different times during the program.
! It makes the program simpler to understand as the code
is grouped together into chunks.
Defining a subprogram and passing variables
between subprograms
Below is a simple program that we would normally create
without subprograms but have written it with subprograms
so you can see how they work:
This program uses three subprograms get_name(), print_Msg() and main().
The get_name() subprogram will ask the user to input their name and then it will return
the value of the variable “user_name” so that it can be used in another subprogram. This is
very important. If you do not return the values, then the values of any variables that were
created or altered in that subprogram cannot be used elsewhere in your program.
100 Challenges 118 - 123: Subprograms
The print_Msg() subprogram will display the message “Hello” and then the user name.
The variable “user_name” appears in the brackets as the current value of the variable is
being imported into the subprogram so it can be used.
The main() subprogram will get the user_name from the get_name() subprogram
(using the variable user_name) as this was returned from the get_name() subprogram. It
will then use that user_name variable in the print_Msg() subprogram.
The last line “main()” is the actual program itself. All this will do is start the main()
subprogram running.
Obviously, there is no need to create such a convoluted way of performing what is in fact a
very simple program, but this is only used as an example of how subprograms are laid out
and variables can be used and passed between the subprograms.
Please note: Python does not like surprises, so if you are going
to use a subprogram in a program, Python must have read the
“def subprogram_name()” line before so it knows where to
go to find it. If you try to refer to a subprogram before Python has
read about it, it panics and will crash. When calling a
subprogram, the subprogram must be written above the section
of code you use to call it. Python will read from the top down and run the first line it comes
across that has not been indented and does not start with the word def. In the program
above this would be main().
Remember: never surprise Python as it
will not like it - good advice for life
generally.
Challenges 118 - 123: Subprograms 101
Example Code
The following examples are all part of the same program and would be displayed in the
order shown here.
def get_data():
user_name = input(“Enter your name: ”)
user_age = int(input(“Enter your age: ”))
data_tuple = (user_name, user_age)
return data_tuple
Defines a subprogram called “get_data()” which will ask the user for their name and
age. As we want to send more than one piece of data back to the main program for other
parts of the program to use, we have combined them together. The return line can only
return a single value, which is why we combined the user_name and user_age variables
into a tuple (see page 58) called data_tuple.
def message(user_name,user_age):
if user_age <= 10:
print(“Hi”, user_name)
else:
print(“Hello”, user_name)
Defines a subprogram called message() which uses two variables that have previously
been defined (user_name and user_age).
def main():
user_name,user_age = get_data()
message(user_name,user_age)
Defines a subprogram called main() which obtains the
two variables from the get_data() subprogram. These
must be labelled in the same order as they were
defined in the tuple. It then calls the message()
subprogram to run with the two variables.
main()
Runs the main() subprogram.
102 Challenges 118 - 123: Subprograms
Challenges
118 119
Define a subprogram that will ask the user to Define a subprogram
enter a number and save it as the variable that will ask the user to
“num”. Define another subprogram that will pick a low and a high
number, and then
use “num” and count from 1 to that number.
generate a random
120
number between those
two values and store it in
Display the following menu to the user: a variable called
“comp_num”.
Define another
subprogram that will
If they enter a 1, it should run a subprogram that will give the instruction “I am
generate two random numbers between 5 and 20, and thinking of a number…”
ask the user to add them together. Work out the correct and then ask the user to
answer and return both the user’s answer and the guess the number they
correct answer. are thinking of.
If they entered 2 as their selection on the menu, it Define a third
should run a subprogram that will generate one number subprogram that will
between 25 and 50 and another number between 1 and check to see if the
25 and ask them to work out num1 minus num2. This comp_num is the same
way they will not have to worry about negative answers. as the user’s guess. If it
Return both the user’s answer and the correct answer. is, it should display the
message “Correct, you
Create another subprogram that will check if the user’s
answer matches the actual answer. If it does, display win”, otherwise it should
“Correct”, otherwise display a message that will say keep looping, telling the
“Incorrect, the answer is” and display the real answer. user if they are too low or
too high and asking them
If they do not select a relevant option on the first menu to guess again until they
you should display a suitable message. guess correctly.
121
Create a program that will allow the user to easily manage a list of names. You should
display a menu that will allow them to add a name to the list, change a name in the
list, delete a name from the list or view all the names in the list. There should also be a
menu option to allow the user to end the program. If they select an option that is not
relevant, then it should display a suitable message. After they have made a selection
to either add a name, change a name, delete a name or view all the names, they
should see the menu again without having to restart the program. The program
should be made as easy to use as possible.
Challenges 118 - 123: Subprograms 103
122 Including menus
Create the following menu: helps make the
program easier to
If the user selects 1, allow them to add to a file
called [Link] which will store their name
and salary. If they select 2 it should display all
records in the [Link] file. If they select 3 it
should stop the program. If they select an
incorrect option they should see an error
message. They should keep returning to the
menu until they select option 3.
123
In Python, it is not technically possible to directly
delete a record from a .csv file. Instead you need
to save the file to a temporary list in Python,
make the changes to the list and then overwrite
the original file with the temporary list.
Change the previous program to allow you to do
this. Your menu should now look like this: