A program is used by a space exploration agency to track and analyze telemetry data.
The program reads spacecraft data from an input file. Each line in telemetry.txt contains data in the format: spacecraft_name,mission_status.
The program counts the total number of spacecraft and the number of active spacecraft (where mission_status is "Active").
The program also stores the names of 8 upcoming deep-space missions in an array. It writes these mission names in lowercase to a different file named missions.txt, with one name per line.
The program should produce the following output on the screen:
Total spacecraft: 85 Active spacecraft: 24
Wrote 8 mission names to file
The source code for Q03.py is provided below:
# Q03.py - Spacecraft Telemetry Program
# Initialize counters
total_spacecraft = 0
active_spacecraft = 0
# Read telemetry data from file
# (uncomment the correct line below by removing the '#')
# telemetry_file = open("telemetry.txt", "w")
# telemetry_file = open("telemetry.txt", "r")
for line in telemetry_file:
# Strip newline and split data (format: spacecraft_name,mission_status)
data = line.strip().split(",")
total_spacecraft = total_spacecraft + 1
# AMEND THIS LINE to check if the mission_status is "Active"
if data[0] == "Active":
active_spacecraft = active_spacecraft + 1
telemetry_file.close()
# Array of 8 upcoming deep-space missions
missions = ["Artemis", "Europa Clipper", "Psyche", "Lucy", "Juice", "Viper", "Dragonfly", "Hera"]
# Write mission names to a new file in lowercase
# (uncomment the correct line below by removing the '#')
# mission_file = open("missions.txt", "w")
# mission_file = open("missions.txt", "r")
for mission in missions:
# AMEND THIS LINE to write the mission name in lowercase with a newline character
mission_file.write(mission + "
")
mission_file.close()
# Print the final output
# AMEND THIS LINE to output the correct counts (ensure no data type errors occur)
print("Total spacecraft: " + total_spacecraft + " Active spacecraft: " + active_spacecraft)
print("Wrote " + str(len(missions)) + " mission names to file")
Amend the code to make the program work and produce the correct output.
You will need to:
# symbol)Do not change the names of the variables or files.
Save your amended code as Q03FINISHED.py
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.