Courseiva
Question 41 of 498
Data Types, Variables, Basic I/O and OperatorshardMultiple ChoiceObjective-mapped

PCEP Practice Question: Data Types, Variables, Basic I/O and Operators

A junior developer created a Python script to calculate the average of three quiz scores entered by the user. The script reads three numbers using input(), converts them to float, calculates the sum, and divides by 3. However, when a user enters a non-numeric value like 'ten', the script crashes with a ValueError. The developer needs to modify the script to handle such errors gracefully, allowing the user to re-enter the invalid input until a valid number is provided. Which approach should the developer implement to meet this requirement most effectively while following Python best practices?

⚠ Common exam trap

Python Institute often tests the distinction between a single try-except (which only catches one error) and a looped try-except (which retries until valid input is given), leading candidates to pick Option C because it mentions 'try-except' and 'looping' but lacks the explicit `while True` structure required for repeated prompting.

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

Implement a while True loop that attempts to convert the input inside a try block. If successful, break out of the loop; if ValueError occurs, print an error message and continue the loop.

It uses a `while True` loop with a `try` block to repeatedly attempt conversion of user input to `float`. If a `ValueError` is raised (e.g., for non-numeric input like 'ten'), the exception is caught, an error message is printed, and the loop continues until valid input is provided. This pattern follows Python best practices for input validation by separating the conversion logic from the loop control and avoiding reliance on fragile string checks like `.isdigit()`.

Answer analysis

Option-by-option breakdown

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

  • Implement a while True loop that attempts to convert the input inside a try block. If successful, break out of the loop; if ValueError occurs, print an error message and continue the loop.

    Why this is correct

    This is the standard pattern for input validation, handling both integers and floats.

  • Use an if statement to check if the input is numeric using .isdigit(), and if not, ask again.

    Why it's wrong here

    isdigit() only works for integers, not floats like '3.5'.

  • Use a try-except block to catch ValueError and prompt the user again, looping until valid input is provided.

    Why it's wrong here

    This is too vague; it doesn't specify the loop structure, which is essential for re-prompting.

  • Use a function that returns a default value of 0 if the input is invalid, to avoid script crashes.

    Why it's wrong here

    Using a default value can mask errors and may produce incorrect averages.

About these practice questions

Courseiva creates original exam-style practice questions with explanations and wrong-answer analysis. It does not publish real exam questions, exam dumps, or protected exam content. Learn why practice questions differ from exam dumps →

How Courseiva writes practice questions · Editorial policy

Same concept, more angles

2 more ways this is tested on PCEP

These questions test the same concept from different angles. Work through them to make sure you can recognise it however the exam phrases it.

Variation 1. A developer is writing a script to process user input. The script should repeatedly ask for a number until a valid integer is entered. Which loop structure is most appropriate?

medium
  • A.while loop with a condition
  • B.for loop
  • C.do-while loop
  • D.Recursion

Why A: A while loop with a condition is the most appropriate because it allows the script to repeatedly prompt for input until a valid integer is entered, checking the condition before each iteration. In Python, the while loop continues as long as the condition evaluates to True, making it ideal for indefinite iteration where the number of repetitions is unknown beforehand. This pattern is commonly used with a try-except block to validate integer conversion.

Variation 2. A company runs a script that processes user input temperatures. The script expects integers but sometimes users enter floats. The current code is: temp = int(input("Enter temperature: ")). When a user enters "36.5", the script crashes with a ValueError. The developer needs to modify the script to handle both integer and float inputs gracefully, converting the input to an integer (by truncation) for processing. Which of the following is the best course of action?

hard
  • A.Use temp = int(float(input()))
  • B.Use input with a regex check to ensure only integers are entered.
  • C.Use a try-except block to catch ValueError and ask the user to re-enter.
  • D.Use float(input()) and do not convert to int, store as float.

Why A: It first converts the input string to a float (handling both integer and float strings), then truncates the float to an integer via int(). This gracefully processes inputs like '36.5' without crashing, meeting the requirement to truncate rather than round.

Last reviewed: Jun 30, 2026

Question Discussion

Share a tip, memory trick, or ask about the reasoning behind this question. Do not post real exam questions, leaked content, braindumps, or copyrighted exam material. Comments are moderated and may be removed without notice.

Loading comments…

Sign in to join the discussion.

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.