Programming techniques Worksheet 1 Programmin
Programming techniques Worksheet 1 Programmin
1. All IDEs have slightly different facilities, but you can get an idea of them by trying out a
built-in debugging module in Python.
In this exercise you will try out the TRACE facility, which enables you to step through the
program a line at a time and display the value of variables.
Open your IDE and type the following Python program:
import pdb
a = 5
b = 10
c = 6
answer1 = a + b * c
print ("answer1 =", answer1)
answer2 = (a + b) * c
print ("answer2 =", answer2)
import pdb imports the Python DeBugging module. At the moment, the program does not
make use of the module.
Run the program. It should print
answer1 = 65
answer2 = 90
(a) Set a trace, starting just before the line b = 10.
Insert the statement pdb.set_trace() between lines a = 5 and b = 10
Save and run the program again. When the program encounters the line
pdb.set_trace(), it will stop, display the current statement (the line that will execute
next), and wait for your input.
You will see the pdb prompt,
(Pdb)
(b) Execute the next statement by typing n at the pdb prompt.
You can type n repeatedly, and it will execute one line at a time. Or, you can just press
Enter, which will repeat the previous command.
(c) Print the value of the variables by typing p <variable name>
e.g. p answer1
If you have not yet reached the line where answer1 is defined, you will get an error
message.
(d) Turn off the pdb prompt by typing c (for “continue”)
You can practise these commands on other programs that you write. For more
instruction, go to
https://pythonconquerstheuniverse.wordpress.com/2009/09/10/debugging-in-python/
1
Worksheet 1 Programming Basics
Unit 11 Programming techniques
Task 2
2. Write an algorithm that will calculate the amount of paint required to paint a room. The user
will enter the dimensions of the room, the total dimensions of the unpaintable areas (such
as windows, doors or brickwork) and the number of coats of paint required.
You can get some handy tips from the site below:
https://www.dulux.co.uk/en/decorating-tips-and-advice/how-to-calculate-the-right-amount-
of-paint
Task 3
3. Write pseudocode for a program which calculates the number of miles per gallon a car is
doing. The user will input
the car mileage the last time the car was filled
the car mileage now
the total number of litres taken to fill the tank
(ii) an integer
Task 4
4. Write an algorithm using pseudocode that asks the user to input the number of students
and the number of books to be equally divided between them. Calculate and output the
number of books that each student will receive and the number left over.
5. Write pseudocode for an algorithm that prompts the user to enter a name, uses a string
function to find its length and then tells the user how long the name is.
Extension task
Write the program for Task 2, calculating litres of paint required. Practise using the debugging
facilities in Python or an alternative language that you are using, or in the IDE itself.