22
0/15

A program is used in a garden nursery to manage plant data.

The program reads plant data from an input file. Each line in plants.txt contains data in the format: plant_name,plant_type.

The program counts the total number of plants and the number of plants with the type "Rose".

The program also stores the names of 10 evergreen shrubs in an array. It writes these shrub names in uppercase to a different file named shrubs.txt, with one name per line.

The program should produce the following output on the screen:

Total plants: 42 Rose plants: 7
Wrote 10 shrub names to file

The source code for Q03.py is provided below:

# Q03.py - Garden Nursery Program

# Initialize counters
total_plants = 0
rose_plants = 0

# Read plant data from file
# (uncomment the correct line below by removing the '#')
# plant_file = open("plants.txt", "w")
# plant_file = open("plants.txt", "r")

for line in plant_file:
    # Strip newline and split data (format: plant_name,plant_type)
    data = line.strip().split(",")
    total_plants = total_plants + 1

    # AMEND THIS LINE to check if the plant_type is "Rose"
    if data[0] == "Rose":
        rose_plants = rose_plants + 1

plant_file.close()

# Array of 10 evergreen shrubs
shrubs = ["Holly", "Boxwood", "Yew", "Euonymus", "Juniper", "Laurel", "Privet", "Arborvitae", "Skimmia", "Camellia"]

# Write shrub names to a new file in uppercase
# (uncomment the correct line below by removing the '#')
# shrub_file = open("shrubs.txt", "w")
# shrub_file = open("shrubs.txt", "r")

for shrub in shrubs:
    # AMEND THIS LINE to write the shrub name in uppercase with a newline character
    shrub_file.write(shrub + "
")

shrub_file.close()

# Print the final output
# AMEND THIS LINE to output the correct counts (ensure no data type errors occur)
print("Total plants: " + total_plants + " Rose plants: " + rose_plants)
print("Wrote " + str(len(shrubs)) + " shrub names 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 uppercase transformations and type conversions are implemented correctly.

Do not change the names of the variables or files.

Save your amended code as Q03FINISHED.py

[15]

Develop code Questions

Practise Edexcel GCSE Computer Science Develop code with exam-style questions for GCSE Computer Science. 57 questions 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, matched to the Edexcel GCSE Computer Science (1CP2) specification and written in Paper 1 and Paper 2 style. Every question includes a full worked solution and mark scheme, so you can see where marks are awarded rather than just whether you got the answer right.

PreviousNext

Develop code Questions

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