0% found this document useful (0 votes)
6 views2 pages

Programming Basics Worksheet Guide

The document contains a worksheet focused on programming basics, including debugging in Python using the pdb module, writing algorithms for calculating paint requirements, and pseudocode for various tasks such as calculating miles per gallon and distributing books among students. It provides step-by-step instructions for using debugging tools and encourages practice through extensions. Additionally, it includes links for further resources on paint calculation and debugging techniques.

Uploaded by

19okhan
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views2 pages

Programming Basics Worksheet Guide

The document contains a worksheet focused on programming basics, including debugging in Python using the pdb module, writing algorithms for calculating paint requirements, and pseudocode for various tasks such as calculating miles per gallon and distributing books among students. It provides step-by-step instructions for using debugging tools and encourages practice through extensions. Additionally, it includes links for further resources on paint calculation and debugging techniques.

Uploaded by

19okhan
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Worksheet 1 Programming Basics

Unit 11 Programming techniques

Worksheet 1 Programming Basics


Task 1

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
[Link]

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.

Assume that 1 litre of paint covers 11 sq m.

You can get some handy tips from the site below:

[Link]
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

n.b. There are 0.22 gallons in a litre, or 4.546 litres in a gallon

Which of the identifiers in your program could you define as

(i) a constant? (What is the advantage of doing this?)

(ii) an integer

(iii) a real (decimal) number?

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.

Common questions

Powered by AI

When converting user input data for a miles-per-gallon calculation, consider data types, as input typically captures strings which need conversion to numerical types. Mileage and litre inputs should be integers, while conversion rates and the resulting calculations for miles per gallon should use floats or 'reals' for precision. Ensure consistent units across data values and handle conversions, such as from litres to gallons, accurately. This ensures the algorithm's accuracy and effectiveness .

Pseudocode aids in sharing algorithms with others by providing a language-neutral, simple way to describe algorithms, making it accessible to anyone with programming experience. It facilitates communication of logic and algorithm design without getting bogged down by syntax of specific programming languages. However, its limitations include lack of standardization, potential ambiguity without specific syntactical rules, and inability to run or test directly, requiring programmers to mentally convert pseudocode to actual code .

To calculate miles per gallon using pseudocode, you input the previous and current car mileage to derive miles driven, and the litres needed for a full tank to convert to gallons. Calculations include converting litres to gallons (using 4.546 litres per gallon) and dividing the miles driven by the gallons used. Consider using a constant for the litre-to-gallon conversion rate and 'real' for the resulting miles per gallon for precision, while mileage inputs can be integers. This careful choice of data types ensures accuracy and efficiency in the computation .

The TRACE facility in Python's debugger (pdb) can be used to understand program execution by allowing you to step through a program line by line. By using the 'pdb.set_trace()' command, the program execution is paused, and you can observe the current statement and the values of variables at that point. You can execute each subsequent line by typing 'n', and review variable values with 'p <variable name>'. This technique helps in analyzing how the program flows and the state of data at each step, which is crucial for debugging .

Defining identifiers as constants is beneficial because it prevents their values from being modified, which reduces errors and aids in code readability. Typically, values that remain unchanged throughout program execution, such as conversion rates (e.g., 0.22 gallons per litre), should be treated as constants. This practice clarifies the intent of using these values, makes the code easier to maintain, and helps prevent accidental modifications that could lead to incorrect results .

To ensure pseudocode aligns with programming logic for name length computation, clearly outline steps such as taking input, using string functions to calculate length, and outputting results. Define variables for inputs and use predefined string length functions, mirroring programming constructs. Check for edge cases, such as empty strings, and document assumptions. Translate pseudocode into testable code effectively by maintaining logical flow and simplicity, ensuring conceptual fidelity to programming practices .

Accurate calculation in algorithm design for division problems, such as distributing books among students, requires clear logic for division and modulus operations. Use integer division to calculate equal distribution and modulus to find the remainder. Ensure input validation to handle cases where divisor is zero or non-integer values are provided. Convert pseudocode to program code accurately, checking for off-by-one errors in logic, and verify calculations with example inputs to ensure correctness .

'set_trace()' in Python is effective for debugging by allowing program execution to pause, thus enabling the inspection of the environment and control flow at critical points. It is especially beneficial in scenarios where complex logical flows occur, or unexpected variable values arise, allowing developers to examine the execution path and state. It's particularly useful in debugging loops, recursive functions, and sections with conditional branches, facilitating quick identification and resolution of issues .

The 'c' command in the Python debugger should be used when you are ready to resume normal program execution after stepping through the lines of code or inspecting variables. This command terminates the debugging session by continuing the program until it either completes or encounters another breakpoint. Thus, it's particularly useful when you have completed the immediate checks or inspection needed and want the program to proceed without further interruption .

To design an algorithm for calculating the amount of paint required, the steps are: 1) Input the dimensions of the room. 2) Input the dimensions of unpaintable areas like windows. 3) Calculate the total paintable area by subtracting unpaintable area from the total wall area. 4) Input the number of coats needed. 5) Calculate the total area to be painted by multiplying the paintable area by the number of coats. 6) Divide this total area by the coverage per liter (11 sq m) to find the liters of paint required. This algorithm combines inputs and calculations systematically to provide the correct amount of paint needed .

You might also like