PCEP Practice Question: Data Types, Variables, Basic I/O and Operators
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?
⚠ Common exam trap
Python Institute often tests the misconception that int() can directly parse a string containing a decimal point, leading candidates to overlook the need for an intermediate float conversion.
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
✓
Use temp = int(float(input()))
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.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✓
Use temp = int(float(input()))
Why this is correct
Correct; float() accepts both int and float strings, int() truncates.
- ✗
Use input with a regex check to ensure only integers are entered.
Why it's wrong here
Rejects valid floats; user experience poor.
- ✗
Use a try-except block to catch ValueError and ask the user to re-enter.
Why it's wrong here
A try-except block that catches ValueError and prompts re-entry does not solve the requirement to convert a float input to an integer by truncation; it merely rejects the float and forces the user to type an integer, which is a different workflow. This approach is tempting because try-except is the standard pattern for handling malformed input in Python, and it would be correct if the goal were to reject non-integer values entirely rather than to accept and truncate floats.
- ✗
Use float(input()) and do not convert to int, store as float.
Why it's wrong here
Changes data type, may not be acceptable for integer processing.
Go deeper
Related to this question
About these practice questions
This PCEP question is part of Courseiva's 498-question bank — original exam-style content with full explanations and wrong-answer analysis, never real exam questions or exam dumps. Learn why practice questions differ from exam dumps →
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.