Skip to content
MathsGenie logo
Quick links
Open app

Course home

  1. IGCSE
  2. Computer Science Edexcel
  3. Question bank

Develop code

EasyMediumHard
123456789101112131415161718192021222324252627
Question 23

Marcus runs a local courier business and wants to write a software tool to audit the formatting of tracking codes stored in a text document.

The text file tracking_codes.txt contains one tracking code on each line.

He wants a program to check each tracking code to ensure that:

  • the first character is a lowercase letter
  • if the first character is a lowercase letter, the code must also contain at least one digit (0–9)

If a tracking code does not meet these requirements, the program must:

  • display the invalid tracking code
  • increment a counter tracking the number of non-compliant codes

After all tracking codes in the file have been checked, the program must display the total count of non-compliant codes.

Write an algorithm, in Python or pseudocode, to solve Marcus's problem. You can use the incomplete program structure provided below to help you write your solution.

# Open the file
file = open("tracking_codes.txt", "r")
invalid_count = 0

# Loop through each line in the file
for line in file:
    code = line.strip()
    # Write your validation logic below

# Close the file and display the final count
file.close()
[9]

Develop code Questions

  1. IGCSE
  2. /Computer Science
  3. /Develop code