Courseiva
Computer Programming and Python FundamentalshardMultiple ChoiceObjective-mapped

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

    int conversion fails for decimal values; also blank lines not handled.

  • 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

    Handles all non-numeric lines, warns about skipped lines.

  • 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

    Skips blank lines but 'Absent' is not blank, so ValueError still occurs.

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

    Why it's wrong here

    isdigit() returns False for negative numbers and floats; 'Absent' is not digit, but also '85.5' would be skipped incorrectly.

About these practice questions

Courseiva writes every PCEP question from scratch — 498 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.