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

Techniques to Maintain Programs

How do you write programs that are easy to maintain?

  • Easy to maintain programs are written using techniques that make code easy to read

  • Programmers should be consistent with the use of techniques such as:

    • Layout – spacing between sections

    • Indentation – clearly defined sections of code

    • Comments – explaining key parts of the code

    • Meaningful variable names – describe what is being stored

    • White space – adding suitable spaces to make it easy to read

  • When consistency is applied, programs are easy to maintain

Example easy to maintain program

Python code

def calculate_area_of_triangle(base, height):

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

“””

Calculates the area of a triangle given its base and height.

Inputs:

base (float): The length of the triangle’s base (positive value).

height (float): The height of the triangle from the base (positive value).

Returns:

The calculated area of the triangle (float).

Raises:

ValueError: If either base or height is non-positive.

“””

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

if base <= 0 or height <= 0:

raise ValueError("Base and height must be positive values.")

area = 0.5 base height

return area

def main():

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

# Prompts the user for triangle base and height, calculates and prints the area

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

try:

base = float(input("Enter the base of the triangle: "))

height = float(input("Enter the height of the triangle: "))

# Call the area calculation function

area = calculate_area_of_triangle(base, height)

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

except ValueError as error:

print(f"Error: {error}")

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

# Main program

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

main()

Responses

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