Read, Write, Analyse & Refine Programs (Edexcel GCSE Computer Science)

Revision Note

Robert Hampton

Written by: Robert Hampton

Reviewed by: James Woodhouse

Read, Write, Analyse & Refine Programs

  • With the exception of the final question, which requires students to design and write a program from scratch, questions on paper 2 require students to analyse and refine given code by:

    • Correcting errors

    • Adding or rearranging lines

    • Selecting the correct line

    • Improving readability

Correcting errors

  • Find and correct the errors in this program

Python code - contain errors

# -----------------------------------------------------------------------

# This program calculates the area of a rectangle

# -----------------------------------------------------------------------

lenght = float(input("Enter the lenght of the rectangle: "))

width = float(input("Enter the width of the rectangle: "))

area = lenght * width

print"The area of the rectangle is:", area)

if area > 100:

print("This is a large rectangle!")

  • This program contain 4 errors:

    • Syntax error - typo in variable name lenght (should be length)

    • Incorrect use of misspelled variable lenght in area calculation

    • Missing bracket in first print statement

    • Incorrect indentation for the if statement

Python code - corrected errors

# -----------------------------------------------------------------------

# This program calculates the area of a rectangle

# -----------------------------------------------------------------------

length = float(input("Enter the length of the rectangle: "))

width = float(input("Enter the width of the rectangle: "))

area = length * width

print("The area of the rectangle is:", area)

if area > 100:

print("This is a large rectangle!")

Adding or rearranging lines

  • Add lines to the calculate_grade() function to assign letter grades based on the score:

    • 90 or above = A

    • 80 - 89 = B

    • 70 - 79 = C

    • Below 70 = F ("Fail")

Python code - incomplete

def calculate_grade(score):

# -----------------------------------------------------------------------

# This function takes a score as input and returns the corresponding letter grade

# -----------------------------------------------------------------------

def main():

# -----------------------------------------------------------------------

# This function gets user input for a score and calls the calculate_grade function

# -----------------------------------------------------------------------

score = int(input("Enter your score (0-100): "))

grade = calculate_grade(score) # Function call

print("Your grade is: ", grade)

# -----------------------------------------------------------------------

# Main program

# -----------------------------------------------------------------------

main()

Python code - Complete

def calculate_grade(score):

# -----------------------------------------------------------------------

# This function takes a score as input and returns the corresponding letter grade

# -----------------------------------------------------------------------

if score >= 90:

return "A"

elif score >= 80:

return "B"

elif score >= 70:

return "C"

else:

return "F"

def main():

# -----------------------------------------------------------------------

# This function gets user input for a score and calls the calculate_grade function

# -----------------------------------------------------------------------

score = int(input("Enter your score (0-100): "))

grade = calculate_grade(score) # Function call

print("Your grade is: ", grade)

# -----------------------------------------------------------------------

# Main program

# -----------------------------------------------------------------------

main()

Improving readability

  • Improve the readability of the following program by adding:

    • Layout

    • White space

    • Comments

    • Indentation

Python code - poor readability

total_bill = float(input("Enter the total bill amount: "))

VAT = 0.20

tip_prompt = input("Do you want to add a tip? (y/n): ").lower()

if tip_prompt == 'y':

tip_percent = float(input("Enter the tip percentage (e.g., 15 for 15%): "))

tip_amount = total_bill * (tip_percent / 100)

total_with_tip = total_bill + tip_amount

else:

total_with_tip = total_bill

VAT = total_with_tip * VAT

final_total = total_with_tip + VAT

print("Your final bill total is: £", final_total)

Python code - good readability

def calculate_bill_total(bill_amount, VAT, tip_percent=0):

# -----------------------------------------------------------------------

"""

This function calculates the total bill amount including VAT and optional tip

Variables:

bill_amount (float): The original bill amount.

VAT(float): The tax rate (e.g., 0.20 for 20%).

tip_percent (float, optional): The tip percentage (e.g., 15 for 15%). Defaults to 0.

Returns:

float: The total bill amount including VAT and tip.

"""

# -----------------------------------------------------------------------

subtotal_with_tip = bill_amount + (bill_amount * tip_percent / 100)

VAT = subtotal_with_tip * VAT

final_total = subtotal_with_tip + VAT

return final_total

# -----------------------------------------------------------------------

# Get user input

# -----------------------------------------------------------------------

bill_amount = float(input("Enter the total bill amount: "))

VAT = 0.20 # Assume VAT rate of 20%

# -----------------------------------------------------------------------

# Ask about tip and calculate total

# -----------------------------------------------------------------------

tip_prompt = input("Do you want to add a tip? (y/n): ").lower()

if tip_prompt == 'y':

tip_percent = float(input("Enter the tip percentage (e.g., 15 for 15%): "))

else:

tip_percent = 0

final_total = calculate_bill_total(bill_amount, VAT, tip_percent)

# -----------------------------------------------------------------------

# Print the final bill total

# -----------------------------------------------------------------------

print("Your final bill total is: £", final_total)

Last updated:

You've read 0 of your 5 free revision notes this week

Sign up now. It’s free!

Join the 100,000+ Students that ❤️ Save My Exams

the (exam) results speak for themselves:

Did this page help you?

Robert Hampton

Author: Robert Hampton

Expertise: Computer Science Content Creator

Rob has over 16 years' experience teaching Computer Science and ICT at KS3 & GCSE levels. Rob has demonstrated strong leadership as Head of Department since 2012 and previously supported teacher development as a Specialist Leader of Education, empowering departments to excel in Computer Science. Beyond his tech expertise, Robert embraces the virtual world as an avid gamer, conquering digital battlefields when he's not coding.

James Woodhouse

Author: James Woodhouse

Expertise: Computer Science

James graduated from the University of Sunderland with a degree in ICT and Computing education. He has over 14 years of experience both teaching and leading in Computer Science, specialising in teaching GCSE and A-level. James has held various leadership roles, including Head of Computer Science and coordinator positions for Key Stage 3 and Key Stage 4. James has a keen interest in networking security and technologies aimed at preventing security breaches.