Courseiva

PCEP Computer Programming and Python Fundamentals Practice Question

You are an IT support specialist for a university. A professor uses a Python script that analyzes exam scores from a text file. The script calculates the average score and prints it. Recently, the script outputs 'NaN' instead of a number. The relevant code is: scores = [float(line.strip()) for line in open('scores.txt')]; average = sum(scores) / len(scores); print(average). You inspect the scores.txt file and find that one line contains the word 'Absent' and another line is blank. The professor wants the script to ignore non-numeric lines and blank lines, and also print a warning if any line was skipped. Which of the following modifications to the script best achieves this?

⚠ Common exam trap

The PCEP exam often tests the misconception that `isdigit()` or simple string emptiness checks are sufficient for numeric validation, but they fail for floats, negative numbers, or non-numeric text like 'Absent'.

Answer choices

Why each option matters

Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.

Correct answer & explanation

✓

Open the file, iterate over lines, use try-except to convert to float, if successful append to list else increment a skip counter. At the end, print the average and the number of skipped lines.

It uses a try-except block to safely attempt conversion of each line to float, incrementing a skip counter for lines that fail (e.g., 'Absent' or blank). After processing, it computes the average only from successfully converted scores and prints both the average and the number of skipped lines, meeting the professor's requirements exactly.

Answer analysis

Option-by-option breakdown

For each option: why learners choose it and why it is or isn't the right answer here.

  • ✗

    Read all lines, filter with a lambda that checks if line can be converted to int, then convert to float.

    Why it's wrong here

    A lambda testing int conversion rejects decimal scores like '87.5' and negative values, and the filter silently drops lines without printing the required warning. It is tempting because filtering with a conversion test handles 'Absent' and blank lines, which would suffice if all scores were whole numbers and no warning were required.

  • ✓

    Open the file, iterate over lines, use try-except to convert to float, if successful append to list else increment a skip counter. At the end, print the average and the number of skipped lines.

    Why this is correct

    Iterating line by line with try-except isolates each conversion, so 'Absent' and blank lines raise ValueError and are skipped rather than poisoning the whole list, which is what produced NaN. The skip counter satisfies the professor's requirement to warn about ignored lines, and the average is computed only from valid floats.

  • ✗

    Use list comprehension with condition if line.strip() != '': scores = [float(line.strip()) for line in open('scores.txt') if line.strip() != '']

    Why it's wrong here

    Testing only for empty strings still passes 'Absent' to float(), raising ValueError rather than skipping it, and no warning is printed. It is tempting because the blank-line check is a genuine part of the fix, and it would work if the file contained only numeric values and empty lines.

  • ✗

    Check if line.strip().isdigit() before conversion, and skip if not.

    Why it's wrong here

    str.isdigit() returns False for valid floats such as '87.5' and for negative scores, so those lines would be wrongly skipped, and it cannot emit the required warning. It is tempting because isdigit() cleanly rejects 'Absent' and blanks, which suits integer-only input where no warning is needed.

About these practice questions

Courseiva writes every PCEP question from scratch — 482 in total, each with an explanation and a wrong-answer breakdown. None are copied from real exams or dumps. Learn why practice questions differ from exam dumps →

How Courseiva writes practice questions · Editorial policy

JA

Written by Johnson Ajibi, MSc IT Security

Senior Network & Security Engineer · founder of Courseiva

This PCEP practice question is part of Courseiva's free Python Institute certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the PCEP exam.