Introduction to Programming Using Python — Question 11

The ABC company has hired you as an intern on the coding team that creates e-commerce applications.
You must write a script that asks the user for a value. The value must be used as a whole number in a calculation, even if the user enters a decimal value.
You need to write the code to meet the requirements.
Which code segment should you use?

Answer options

Correct answer: D

Explanation

The correct answer is D because using int() converts the user's input to an integer, ensuring it is treated as a whole number for calculations. Option A simply retrieves the input as a string, which is not suitable for numerical calculations. Option B converts the input to a float, which allows decimal values, and option C converts the input to a string, neither of which meets the requirement of using a whole number.