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

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)

Responses

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