PCEP-30-02 · Question #261
You want to write a program that asks the user for a value. For the rest of the program you need a whole number, even if the user enters a decimal value. What would you have to write?
The correct answer is B. num = int(float(input('How many do you need?'))). Option B works because input() always returns a string, float() converts that string to a decimal number (handling inputs like "3.7"), and then int() truncates the decimal to give a whole number - so even if the user types "3.7", you get 3. Why the others fail: A - wrapping…
Question
Options
- Anum = str(input('How many do you need?'))
- Bnum = int(float(input('How many do you need?')))
- Cnum = oat(input('How many do you need?'))
- Dnum = int('How many do you need?')
How the community answered
(55 responses)- A11% (6)
- B80% (44)
- C5% (3)
- D4% (2)
Explanation
Option B works because input() always returns a string, float() converts that string to a decimal number (handling inputs like "3.7"), and then int() truncates the decimal to give a whole number - so even if the user types "3.7", you get 3.
Why the others fail:
- A - wrapping
input()instr()is redundant sinceinput()already returns a string; you still have a string, not a number. - C -
oat()is not a Python function (it's missing thefl); this would cause aNameErrorat runtime. - D -
int('How many do you need?')tries to convert the prompt text itself into an integer, completely skipping user input.
Memory tip: Think of it as a two-step pipeline - string → float → int - because you can't jump directly from a string like "3.7" to an integer; Python needs the float as a middle step to parse the decimal point first.
Community Discussion
No community discussion yet for this question.