This tutorial explores strategies for creating reliable and user-friendly programs, essential for success in your OCR GCSE Computer Science Paper 2.
Imagine a program that calculates your age based on a year of birth you input. What happens if you accidentally enter letters instead of numbers? The program could crash! Input validation ensures that user input matches the expected format, preventing unexpected errors.
Techniques:
Example:
Enter your birth year:
Code:
year = input("Enter your birth year: ")
if year.isdigit(): # Check if the input is a digit
year = int(year) # Convert to integer
if year >= 1900 and year <= 2023: # Range check
print("Your age is:", 2023 - year)
else:
print("Invalid birth year. Please enter a valid year.")
else:
print("Invalid input. Please enter a number.")
Authentication verifies the identity of a user before granting access to a program or system.
Methods:
Example:
Username:
Password:
Code:
username = input("Username: ")
password = input("Password: ")
if username == "admin" and password == "secret":
print("Access granted!")
else:
print("Incorrect username or password.")
Maintainable code is easy to understand, modify, and debug.
Key Principles:
Example:
Without Comments and Indentation:
a = 10
b = 5
c = a + b
print (c)
With Comments and Indentation:
# Calculate the sum of two numbers
a = 10 # Assign 10 to variable 'a'
b = 5 # Assign 5 to variable 'b'
c = a + b # Add the values of 'a' and 'b' and store in 'c'
print(c) # Print the result
Testing verifies that your program functions correctly under various conditions.
Types of Tests:
Example:
Normal Data:
Enter your age: 25
Boundary Data:
Enter your age: 0
Enter your age: 120
Erroneous Data:
Enter your age: -5
Enter your age: abc
By implementing these strategies, you can build programs that are robust, reliable, and user-friendly. This knowledge will help you excel in your GCSE Computer Science exam and prepare you for future software development endeavors. Remember, practice makes perfect!