Python - Objective 01 Learn How To Write Structured Programs Workbook
Python - Objective 01 Learn How To Write Structured Programs Workbook
Learntext]
how to write structured programs Craig’n’Dave
Craig’n’Dave
TRY
Sample program:
# My first program
# Main program
OutputText()
PYTHON T I M E
[Header
Learntext]
how to write structured programs Craig’n’Dave
Craig’n’Dave
INVESTIGATE
Understand how the code works: • Programs should start with a comment stating the purpose of the program.
• Comments are identified with the hash # symbol.
# My first program
PYTHON T I M E
[Header
Learntext]
how to write structured programs Craig’n’Dave
Craig’n’Dave
TRY
Sample program:
# Using variables and constants
# Main program
print(Discount(60,50))
PYTHON T I M E
[Header
Learntext]
how to write structured programs Craig’n’Dave
Craig’n’Dave
# Subroutine to calculate price 2. The number 60 is put into Total. The number 50 is put into Rate.
def Discount(Total, Rate): These are known as variables – the name for a piece of memory to store our data.
return Total - (Total * Rate/100)
# Main program 3. The calculation happens here with the result being returned from the subroutine.
print(Discount(60,50))
PYTHON T I M E
[Header
Learntext]
how to write structured programs Craig’n’Dave
Craig’n’Dave
TRY
Sample program:
# Using variables and constants
# Main program
Volume = 330
Time = 4
Heart = FlowRate(Volume, Time)
print("The flow rate of the human heart is", Heart, "ml/s")
PYTHON T I M E
[Header
Learntext]
how to write structured programs Craig’n’Dave
Craig’n’Dave
INVESTIGATE This program calculates the flow rate of blood from the heart.
# Subroutine to calculate price These variables called Volume and Time are different variables to Volume
def FlowRate(Volume, Time): and Time in the Main program. They just happen to have the same name!
return Volume / Time
# Main program You can store the values of constants with meaningful names.
Volume = 330 Note the use of comments helps to explain the units here too.
Time = 4
Heart = FlowRate(Volume, Time)
print("The flow rate of the human heart is", Heart, "ml/s")
PYTHON T I M E
[Header
Learntext]
how to write structured programs Craig’n’Dave
Craig’n’Dave
INVESTIGATE
Learning points from this objective:
• Programs are structured into smaller subroutines.
• Subroutines that do not return any values are called procedures. Subroutines that return values are called functions.
• Code inside subroutines is tabulated. This helps to make it more readable, but it also tells the computer what code belongs to the subroutine.
• Comments are used at the beginning of programs and subroutines to explain their purpose.
Larger programs are often written by teams of programmers, so this helps other people understand your code and for you to remember how it worked too!
• Keywords are the words that the programming language uses for instructions. E.g. print. Keywords are also called reserved words because you cannot use
them for other purposes in your program. e.g. you cannot call a subroutine print.
• Python is case sensitive. This means that keywords must be entered in the correct case.
• Text in a program is called a string. It is a string of characters.
• Strings are qualified with double quote marks. E.g. “This is a string” to show where the text starts and ends.
• Constants are values in a program that can only be changed by the programmer. The program does not change them when it is running.
• Variables are places in memory where values are stored. They are given an identifier to name them.
• Variables have a data type that describes the data they are storing, e.g. string, float etc. This reserves the correct amount of memory for the data.
Specifying the data type is not necessary in Python.
• The name of a variable is called an identifier.
• Different variables can have the same identifier in different subroutines.
• When variables are given a value they are said to be assigned. Giving a variable an initial value is called initialising the variable.
PYTHON T I M E
[Header
Learntext]
how to write structured programs Craig’n’Dave
Craig’n’Dave
INVESTIGATE
Learning points from this objective:
• Strings can be joined to variables. This is known as concatenation.
• When constants or variables are used as data given to subroutines, this is called parameter passing. The data is called a parameter.
• Running a program is called executing a program. Executing one command after another is called a sequence.
• If a program doesn’t work, it is said to have a “bug”. Fixing bugs is called, “debugging”.
PYTHON T I M E
[Header
Learntext]
how to write structured programs Craig’n’Dave
Craig’n’Dave
INVESTIGATE
Program comprehension: ITEM Identify a string in the program.
1. def Sq(X):
2. return X * X
Identify the name of a subroutine in the program.
3. 9 = A
4. print(A,"squared:",Sq(A))
PYTHON T I M E
[Header
Learntext]
how to write structured programs Craig’n’Dave
Craig’n’Dave
INVESTIGATE
Program comprehension: PURPOSE What is the purpose of the comma , in line 4?
1. def Sq(X):
2. return X * X
What does the command print in line 4 do?
3. 9 = A
4. print(A,"squared:",Sq(A))
PYTHON T I M E
[Header
Learntext]
how to write structured programs Craig’n’Dave
Craig’n’Dave
INVESTIGATE
Keywords introduced in this objective:
def subroutine() Starts and names a new subroutine.
print("string", X) Outputs text string concatenated with X to the screen. A comma inserts a space in-between.
Using + instead does not insert a space.
print("Hello",end="") Using end="" prevents a new line between print statements.
print("World")
PYTHON T I M E
[Header
Learntext]
how to write structured programs Craig’n’Dave
Craig’n’Dave
MAKE
# Dice face 5 problem
ooooooooooo
o o def Output5():
o # # o
o # o print("o # o")
o # # o
o o
ooooooooooo print("o # # o")
Output5(
)
print("o # # o")
print("o o")
# Main program
print("ooooooooooo")
print("o o")
To help you get started,
here is all the code you
need with the
statements jumbled up.
PYTHON T I M E
[Header
Learntext]
how to write structured programs Craig’n’Dave
Craig’n’Dave
MAKE
# Temperature converter problem
F = CtoF(C)
return (C * 1.8) + 32
MAKE
Characters problem:
1 point.
Write a program to produce the following output:
The digits are: 0123456789
The characters are: abcdABCD@#!£
The alphanumerics are: 0123456789 abcdABCD@#!£
PYTHON T I M E
[Header
Learntext]
how to write structured programs Craig’n’Dave
Craig’n’Dave
MAKE
Fish tank volume problem:
2 points.
Write a subroutine that will output the volume of a fish tank. To calculate volume in litres, multiply length by depth by height and divide
by 1000.
Microscopy problem:
2 points.
The actual size of a cell viewed under a light microscope is 80μm (micrometres). It measures 4cm on the slide. Calculate the
magnification.
The size on the slide multiplied by 10,000 converts the measurement to μm. Divide this by the actual size to determine the magnification.
PYTHON T I M E
[Header
Learntext]
how to write structured programs Craig’n’Dave
Craig’n’Dave
MAKE
Carpet cost problem:
2 points.
Write a subroutine that outputs the cost of fitting a carpet using 3 parameters:
• Width of the room in metres.
• Length of the room in metres.
• Price of carpet per m2.
Underlay for the carpet will also be required at £3 per m 2.
Grippers will be required at £1/m: width * 2 + length * 2
The fitting fee is £50.
Test the program with a 7 x 6m room and a £17m 2 carpet.
E.g. 7m x 6m room with a £17 carpet = £714 + £126 + £26 + £50 = £916
PYTHON T I M E
[Header
Learntext]
how to write structured programs Craig’n’Dave
Craig’n’Dave
MAKE
Energy bill calculator problem:
3 points.
Write a subroutine that will output the raw cost of energy usage by taking three parameters: previous meter reading, current reading and
calorific value.
The units used equals the current reading minus the previous reading.
kWh = units used * 1.022 * calorific value divided by 3.6.
The calorific value varies a little by each supplier, are governed by legislation and provided by the National Grid. Assume a value of 39.3.
Energy is charged at 2.84p per kWh.
You do not need to format the output because the result is the input to further calculations.
us
sector arc length
from the diameter and arc angle:
i
rad
• The radius of the circle (diameter divided by 2). arc angle
• The area of the circle (3.14 multiplied by the radius squared). diameter
nce
ere
• The circumference of the circle (3.14 multiplied by the diameter).
mf
• The arc length (circumference multiplied by the arc angle), divided by 360.
u
circ PYTHON T I M E
[Header
Learntext]
how to write structured programs Craig’n’Dave
Craig’n’Dave
MAKE
Ball pit problem:
3 points.
The number of balls needed to fill a children’s ball pit can be calculated as: the volume of the ball pit divided by the volume of a single
ball multiplied by the packing density.
The packing density is generally 0.75.
The volume of the ball pit is calculated as: Pi (3.14) * radius 2 * height of the ball pit.
The volume of a ball is calculated as: (4/3) * Pi (3.14) * radius 3.
Write a function to return the volume of the ball pit from two parameters: ball pit radius and ball pit height.
Write a function to return the volume of a ball from one parameter: ball radius.
The program should output the number of balls required to fill a pit with a radius of 1m and height of 0.2m using 0.05m balls.
PYTHON T I M E
[Header
Learntext]
how to write structured programs Craig’n’Dave
Craig’n’Dave
EVALUATE
Review your solutions:
Did you: Self-assess: Yes/No
PYTHON T I M E