Skip to content

Course home

Sign up

Develop code

EasyMediumHard
1234567891011121314151617
Question 10

A program is used by a logistics company to manage drone flight data.

The program reads telemetry data from an input file. Each line in flights.txt contains data in the format: drone_id,flight_status.

The program counts the total number of flights and the number of flights with the status "Active".

The program also stores the codes of 10 regional cargo hubs in an array. It writes these hub codes in lowercase to a different file named hubs.txt, with one code per line.

The program should produce the following output on the screen:

Total flights: 156 Active flights: 23
Wrote 10 hub codes to file

The source code for Q03.py is provided below:

# Q03.py - Drone Telemetry Program

# Initialize counters
total_flights = 0
active_flights = 0

# Read flight data from file
# (uncomment the correct line below by removing the '#')
# flight_file = open("flights.txt", "w")
# flight_file = open("flights.txt", "r")

for line in flight_file:
    # Strip newline and split data (format: drone_id,flight_status)
    data = line.strip().split(",")
    total_flights = total_flights + 1

    # AMEND THIS LINE to check if the flight_status is "Active"
    if data[0] == "Active":
        active_flights = active_flights + 1

flight_file.close()

# Array of 10 regional hubs
hubs = ["LHR", "LGW", "MAN", "STN", "EDI", "BHX", "GLA", "BRS", "NCL", "LPL"]

# Write hub codes to a new file in lowercase
# (uncomment the correct line below by removing the '#')
# hub_file = open("hubs.txt", "w")
# hub_file = open("hubs.txt", "r")

for hub in hubs:
    # AMEND THIS LINE to write the hub code in lowercase with a newline character
    hub_file.write(hub + "
")

hub_file.close()

# Print the final output
# AMEND THIS LINE to output the correct counts (ensure no data type errors occur)
print("Total flights: " + total_flights + " Active flights: " + active_flights)
print("Wrote " + str(len(hubs)) + " hub codes to file")

Amend the code to make the program work and produce the correct output.

You will need to:

  • amend the incorrect or incomplete lines of code
  • choose between the alternative commented lines of code by uncommenting the correct line (removing the # symbol)
  • ensure that lowercase transformations and type conversions are implemented correctly.

Do not change the names of the variables or files.

Save your amended code as Q03FINISHED.py

[14]
Markscheme

Develop code Questions

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

51 exam-style questions on Edexcel GCSE Computer Science Develop code, covering Decomposition and abstraction to solve problems, Read, write, analyse and refine programs, Converting algorithms into programs, Techniques for readable, maintainable code, Identifying and correcting program errors, and Evaluating program fitness and efficiency. Each one has a worked solution and a mark scheme showing where the marks go.

Question bank