Back to 课程

Computer Science GCES EDEXCEL

0% Complete
0/0 Steps
  1. Decomposition And Abstraction Edexcel
    2 主题
  2. Algorithms Edexcel
    11 主题
  3. Truth Tables Edexcel
    3 主题
  4. Binary Edexcel
    6 主题
  5. Data Representation Edexcel
    4 主题
  6. Data Storage And Compression Edexcel
    2 主题
  7. Hardware Edexcel
    5 主题
  8. Software Edexcel
    3 主题
  9. Programming Languages Edexcel
    2 主题
  10. Networks Edexcel
    7 主题
  11. Network Security Edexcel
    2 主题
  12. Environmental Issues Edexcel
    1 主题
  13. Ethical And Legal Issues Edexcel
    3 主题
  14. Cybersecurity Edexcel
    2 主题
  15. Develop Code Edexcel
    6 主题
  16. Constructs Edexcel
    4 主题
  17. Data Types And Data Structures Edexcel
    5 主题
  18. Operators Edexcel
    1 主题
  19. Subprograms Edexcel
    2 主题
课 Progress
0% Complete

Exam code:1CP2

How do you identify, locate & correct program errors?

  • In the exam students are expected to be able to:

    • Fix syntax errors in code

    • Use test data to identify run-time and logic errors which undetected would prevent code from functioning correctly

  • A level-based mark scheme for functionality awards marks for error-free, robust solutions

Examiner Tips and Tricks

Before working through the examples below, it is important you fully understand the different types of errors in programming. To revise them click here

Syntax Errors

Task

  • Locate the syntax errors and correct them so that the program works

Python code – with syntax errors

def generate_username(first_name, last_name)

“””

Generates a username based on the provided first and last name.

Inputs:

first_name (str): User’s first name.

last_name (str): User’s last name.

Returns:

str: A username combining initials and the first 3 characters of the last name.

“””

username = f"{first_name[0]}{last_name[0]}{last_name[:3]}"

return username

def main():

# ———————————————————————–

# Prompts the user for personal information and displays a suggested username

# ———————————————————————–

first_name = imput("Enter your first name: ")

last_name = input("Enter your last name: ")

usdrname = generate_username(first_name, last_name)

print(f"Here's a suggested username: {username}")

# ————————————————————————

# Main program

# ————————————————————————

main

Python code – without syntax errors

def generate_username(first_name, last_name):

“””

Generates a username based on the provided first and last name.

Inputs:

first_name (str): User’s first name.

last_name (str): User’s last name.

Returns:

str: A username combining initials and the first 3 characters of the last name.

“””

username = f"{first_name[0]}{last_name[0]}{last_name[:3]}"

return username

def main():

# ———————————————————————–

# Prompts the user for personal information and displays a suggested username

# ———————————————————————–

first_name = input("Enter your first name: ")

last_name = input("Enter your last name: ")

username = generate_username(first_name, last_name)

print(f"Here's a suggested username: {username}")

# ————————————————————————

# Main program

# ————————————————————————

main()

Syntax errors

  1. Missing semicolon def generate_username(first_name, last_name)

  2. Incorrectly spelt function name first_name = imput("Enter your first name: ")

  3. Typo in variable name usdrname = generate_username(first_name, last_name)

  4. Missing parenthesis main

Logic & Runtime Errors

Task

  • Use suitable test data to identify and locate logic & runtime errors in the following program

Python code

def calculate_area(length, width):

“””

Calculates the area of a rectangle

Inputs:

length (float): The length of the rectangle (positive value)

width (float): The width of the rectangle (positive value)

Returns:

float: The calculated area of the rectangle

Raises:

ValueError: If either length or width is non-positive

“””

if length < 0 or width < 0:

raise ValueError("Length and width must be positive values.")

area = length * width

return area

def main():

# ———————————————————————–

Prompts the user for rectangle dimensions and prints the calculated area

# ———————————————————————–

try:

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

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

# Call the area calculation function

area = calculate_area(length, width)

print(f"The area of the rectangle is approximately {area:.2f} square units.")

except ValueError as error:

print(f"Error: {error}")

# ———————————————————————–

# Main program

# ———————————————————————–

main()

Logic errors

Test number

Test data

Expected outcome

Actual outcome

Changes needed? (Y/N)

1

Length = 5

Width = 5

“The area of the rectangle is approximately 25 square units.”

“The area of the rectangle is approximately 25.00 square units.”

N

2

Length = 10

Width = 0

“Length and width must be positive values.”

“The area of the rectangle is approximately 0.00 square units.”

Y
should not accept 0 input as not positive

  • Logic error located on line if length < 0 or width < 0:

  • Logic error identified in expression < 0, should be <= 0 so that 0 is not accepted as valid input for length or width

Runtime errors

Test number

Test data

Expected outcome

Actual outcome

Changes needed? (Y/N)

1

Length = 10

Width = 10

“The area of the rectangle is approximately 100 square units.”

“The area of the rectangle is approximately 100.00 square units.”

N

2

Length = “abc”

Width = 0

“Program could not convert string to float, try again”

Program crashed

Y
should give error message and ask user to enter again

  • Runtime error located in

try:

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

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

# Call the area calculation function

area = calculate_area(length, width)

print(f"The area of the rectangle is approximately {area:.2f} square units.")

except ValueError as error:

print(f"Error: {error}")

  • Runtime error identified as missing iteration (while loop) so program does not ask user to enter width and height again

  • Corrected code now includes a while loop

while True:

try:

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

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

# Call the area calculation function

area = calculate_area(length, width)


print(f"The area of the rectangle is approximately {area:.2f} square units.")

break # Exit the loop if successful

except ValueError as error:

print(f"Error: {error}")

print("Please enter positive values for length and width.")

Responses

您的邮箱地址不会被公开。 必填项已用 * 标注