PCEP Practice Question: Data Types, Variables, Basic I/O and Operators
What is the correct way to read a floating-point number from user input and store it in a variable?
⚠ Common exam trap
Test-takers frequently confuse the order of operations, thinking they can apply a type conversion method directly on the input string (like `.float()`) or use non-existent syntax like `as float`, instead of wrapping the `input()` call with the `float()` function.
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
✓
x = float(input())
`float(input())` first reads the user input as a string via `input()`, then converts that string to a floating-point number using the `float()` function. This is the standard and only valid way in Python to obtain a float from console input, as `input()` always returns a string.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
x = input(float())
Why it's wrong here
input(float()) calls float() with no argument first, raising TypeError before input ever runs; input() takes a prompt string, not a converter. It is tempting because it superficially chains the two functions needed, and float(input()) would be correct for reading a floating-point value.
- ✗
x = input() as float
Why it's wrong here
Python has no 'as' cast operator; 'as' appears only in imports, with-statements and exception clauses, so this is a syntax error. It is tempting because SQL and C# use similar casting syntax, and float(input()) is the actual construct for converting typed input.
- ✓
x = float(input())
Why this is correct
`float(input())` converts the entered string directly into a floating-point value, satisfying the requirement to read a number rather than text. `input()` always returns a string, so wrapping it in `float()` performs the necessary type conversion in one step, storing the numeric result in `x`.
- ✗
x = input().float()
Why it's wrong here
Strings have no float() method, so calling input().float() raises AttributeError; conversion is a built-in function applied to the string, not a method on it. It is tempting because method chaining reads naturally, and float(input()) is the correct pattern for reading a floating-point number.
Go deeper
Related to this question
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 →
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.