nerdexam
Python_Institute

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

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?

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)
  • A
    11% (6)
  • B
    80% (44)
  • C
    5% (3)
  • D
    4% (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() in str() is redundant since input() already returns a string; you still have a string, not a number.
  • C - oat() is not a Python function (it's missing the fl); this would cause a NameError at 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.

Full PCEP-30-02 Practice