Recent chats
ASKGEEZ Q&A GUIDETechnology & devices

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]

LET’S WORK THROUGH IT

The question, the details, the next step.

Question

I type 7 into input(), but then my calculation behaves as if it is text. Why?

Geez

input() reads a line of input and returns it as a string; it does not automatically turn digits into a number. [S1] So if you enter 7, the value is the text string "7", not the integer 7. The distinction matters because you need a numeric value for arithmetic.

Convert the input explicitly before using it in a calculation. Use int() when the expected value is a whole number, or float() when it may contain a decimal point. The conversion changes the value from text to a numeric type, so your program can use it for arithmetic.

Follow-up question

What does that look like in a small program, and how can I tell the types apart?

Geez

Here is a whole-number example. The variable names and comments show the difference between the original text and the converted integer:

text = input("Enter a whole number: ")  # text is a string

try:
    number = int(text)  # number is an integer
    print("One more is", number + 1)
except ValueError:
    print("Please enter a whole number.")

If you type 7, text holds the string "7", while number holds the integer 7. The calculation adds one to the integer and prints One more is 8. int() constructs an integer from suitable text. If the text cannot be converted, it raises ValueError; the try/except displays the message instead. [S1]

Follow-up question

What if I want to accept a decimal, like 3.5?

Geez

Use float() for an input that may include a decimal point:

text = input("Enter a decimal number: ")
number = float(text)
print("Twice the number is", number * 2)

If you enter 3.5, number is a floating-point value, and the multiplication performs arithmetic. [S1] Choose the conversion based on what the program should accept: int() for a whole-number value, or float() when decimal values are allowed. This example uses a simple decimal representation; it does not define extra input rules, such as accepting commas as decimal separators.

Follow-up question

How do I decide where the conversion belongs, and what should I check when it still fails?

Geez

Keep the result of input() in a clearly named string variable, then convert it when your program needs a numeric value. That makes the distinction visible: text is what the person typed; number is the converted value used for arithmetic. If converting with int() fails, check whether the entered text is a whole-number value and whether int() is the conversion your program needs. Catch ValueError when you want to give the person another prompt or a helpful message. [S1]

A useful mental model is: read text, convert it, then calculate. Do not assume a value is numeric just because it looks like digits on screen. Choose float() instead when your program should accept decimal values, and define any additional input-format rules your program needs.

A practical checklist

  • Store the result of input() in a clearly named text variable.

  • Choose int() for a whole number or float() when decimals are allowed.

  • Convert before performing arithmetic.

  • Handle ValueError if 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.

  1. [S1] Built-in Functions — input and numeric conversions
    docs.python.org · Checked

YOUR QUESTION. YOUR DETAILS.

Let’s work through your situation.

Get a tailored answer or help with your next task. Starts at $1, with a quote before you pay.

Ask your own question
← Back to Geez at Work