Question 1 — Bug Fixing
A program calculates a discount based on the number of two-course meals, three-course meals and drinks ordered by the user.
When the total number of meals exceeds eight, a 15% discount is applied.
When the number of three-course meals exceeds four, a 10% discount is applied.
When the number of two-course meals exceeds two, a 5% discount is applied.
Only one discount can be applied.
When the number of drinks does not match the total number of meals, an error message is displayed. The total must not be calculated or displayed.
Open file Q01.py
Amend the code to:
- fix the syntax error on original line 4
cost2Courses = 15.00 Cost of meals - fix the syntax error on original line 6
cost Drinks = 2.00 - fix the syntax error on original line 10
0 = numDrinks - fix the TypeError on original line 28
total = "num2Courses" * cost2Courses - fix the NameError on original line 29
total = total + num3Courses * cost33Courses - fix the NameError on original line 38
total = 0.95 * tatol - fix the logic error on original line 33
if (num2Courses - num3Courses > 8): - fix the logic error on original line 34
total = 0.85 + total - fix the logic error on original line 40
print ("Total is:", total)
Do not change the functionality of the given lines of code.
Do not add any additional functionality.
Question 2 — Code Completion
A program is required to simulate the countdown to a rocket launch.
The user inputs a number between 1 and 10.
The program counts down from that number.
The wait time between each count is 1.5 seconds and is stored in the program code.
Here is the output of a functioning program, when the user enters the number 3:
Open file Q02.py
Amend the code to:
- import the time library
- create a variable named
countDownand set it to 0 - take the input from the user and convert it to an integer
- ensure the number inputted is valid by using two relational operators and one logical operator
- ensure that the loop stops when it reaches 0
- display
countDownfor the user - call a function in the time library to pause the program for the wait time
- reduce
countDownby 1 - display "Ignition - lift off!" for the user
Do not add any additional functionality.
Question 3 — Code Reordering
A program allows the user to enter a string.
If the string is fewer than three characters long, an error message is displayed and the string is not processed.
Otherwise, the program adds up the ASCII values for each character in the string.
When the total is divisible by four, a four is added to the string.
When the total is divisible by five, a five is added to the string.
When the total is divisible by both four and five, a four is added to the string.
When the total is not divisible by either four or five, a zero is added to the string.
The program displays the total value and the changed string.
The table shows accurate test results for six inputs.
| Input | Output |
|---|---|
| X | String must be three or more characters long |
| XY | String must be three or more characters long |
| cup | 328 cup4 |
| snow | 455 snow5 |
| Ian | 280 Ian4 |
| HAT | 221 HAT0 |
| <empty> | No output, but exits program |
The lines of code in the program are mixed up. The indentation has been done for you.
Open file Q03.py
Amend the code to make the program work and produce the correct output. You will need to rearrange the lines.
Do not change the functionality of the given lines of code.
Do not add any additional functionality.
Question 4 — Code Completion
A program is required to calculate the values in a multiplication square.
Here is an example of a multiplication square:
| × | 1 | 2 | 3 | 4 | 5 |
|---|---|---|---|---|---|
| 1 | 1 | 2 | 3 | 4 | 5 |
| 2 | 2 | 4 | 6 | 8 | 10 |
| 3 | 3 | 6 | 9 | 12 | 15 |
| 4 | 4 | 8 | 12 | 16 | 20 |
| 5 | 5 | 10 | 15 | 20 | 25 |
The number 10, at the intersection of row 2 and column 5, is the result of 2 × 5.
The user enters a number for the sides of the square into the program.
The program has these requirements:
- calculate the values for the multiplication square
- write the values to a new file
- separate values with commas
- do not include the row and column labels
Here are the contents of the output file, when the user enters the number 5:
The program is partially complete.
Open file Q04.py
Amend the code to meet the requirements.
Use the constants, variables and existing code provided.
Do not add any additional functionality.
Question 5 — Turtle Graphics
A program is being developed to display nine coloured circles using turtle graphics.
Here is a greyscale image of the output produced by a fully functional program.
(Refer to the question paper for the reference image showing the turtle graphics output with 4 lines and 9 circles.)
The lines
- The starting point for:
- line A is (−200, 0)
- line B is (0, 200)
- Lines A and B are 400 units long.
- The starting point for:
- line C is (−200, −200)
- line D is (200, −200)
- Lines C and D are at 45 degrees and 566 units long.
- The lines are drawn in black with a width of three units.
The circles
- There are nine coloured circles.
- All circles have a radius of 75 units.
- The position of circle 1 is stored in the variables
xPosandyPos. - The second circle is located at
xPos + 30andyPos + 30. - The circle colours are stored in a one-dimensional array. Each colour is used in sequence. The colours of the circles will be the same each time the program is run.
- The width of the line to draw a circle is a random integer between five and ten. The width of the line may be different for each circle and may be different each time the program is run.
The turtle
- The speed of the turtle must be as quick as possible.
The graphics screen
- The code to create the graphics screen is provided.
- The code to keep the graphics screen open is provided.
Open file Q05.py
Amend the code to create the image shown.
Use comments, white space and layout to make the program easier to read and understand.
Use the constants, variables and existing code provided.
Do not add any additional functionality.
Question 6 — Write from Scratch
A set of records is stored in a two-dimensional array.
The fields in each record are a key and a signal reading.
A valid key:
- has a minimum length of three
- is a group of letters and digits
- ends with a valid check digit
The table shows how to calculate the check digit for any key.
| Key | Sum the digits only in the key | Check digit = right-most digit of sum |
|---|---|---|
| A2B3C5 | 2 + 3 = 5 | 5 |
| A7B30 | 7 + 3 = 10 | 0 |
| A11 | 1 | 1 |
| A274B3 | 2 + 7 + 4 = 13 | 3 |
A valid signal reading must be a real number:
- greater than or equal to 1.0
- less than or equal to 5.0
Some of the records have errors. Eight records have invalid keys. Four records have invalid signals.
Open file Q06.py
Write a program to meet these requirements:
- find and display each invalid record, including the key and the signal
- inform the user if a record has an invalid key (no reason required)
- inform the user if a record has an invalid signal (no reason required)
- work even if the number of records is changed
- include at least one user-devised subprogram
Use comments, white space and layout to make the program easier to read and understand.
Do not add any additional functionality.