Why does `input()` give me text even when I type a number?
Quick answer
input() returns a string, even if the person types digits. Convert that string explicitly before doing arithmetic: use int() for whole numbers or float() for decimal values. If text cannot be converted to an integer, Python raises ValueError; handle that case if you want to show a helpful message instead of stopping the program. [S1]
The question, the details, the next step.
A practical checklist
Store the result of
input()in a clearly named text variable.Choose
int()for a whole number orfloat()when decimals are allowed.Convert before performing arithmetic.
Handle
ValueErrorif unsuitable integer input should produce a helpful message.Keep the original text and converted number in separate variables.
A few more useful answers
Does typing digits make `input()` return an integer?
No. input() returns the line as a string, even when it contains digits. Convert it explicitly with int() or float() before arithmetic. [S1]
When should I use `int()` rather than `float()`?
Use int() when your program expects a whole-number value; use float() when it should accept decimal values. [S1]
What happens if the text cannot be converted to an integer?
int() raises ValueError. Catching that exception lets your program display a useful message instead of stopping at that conversion. [S1]
Sources
References used for this guide.
- [S1] Built-in Functions — input and numeric conversions docs.python.org · Checked
