Hello again. In the last lesson, you evaluated arithmetic expressions and used variables to store calculation results. Those programs still used numbers written directly in the code. Now you will make a program respond to a person at the keyboard.
By the end of this lesson, you will be able to collect input with input(), recognize that the result begins as text, and convert it to an int or float before calculating with it. This is the bridge between a fixed program and an interactive one.
A program can pause and ask for information
Python’s input() function displays a message, waits for the user to type something, and gives the typed value back to the program.
name = input("What is your name? ")
print("Hello,", name)
When you run this program in your browser workspace:
- Python displays
What is your name? - The program pauses.
- You type a response and press Enter or Return.
- Python stores that response in
name. - The
print()call displays the greeting.
The text inside input() is called a prompt. A useful prompt tells the user exactly what to enter and, when helpful, which unit to use.
Compare these prompts:
number = input("Enter a value: ")
minutes = input("How many minutes did you study today? ")
The second one is much more helpful because it explains both the meaning and expected form of the input.
Read the following short section before experimenting. It introduces the prompt, explains where input appears in a browser environment, and shows an early numeric-input program.
2.15. Input — Foundations of Python Programming
Read “Input” from Foundations of Python Programming for the basic model of how input() pauses a program and returns what the user typed.
In Section 2.15, “Input,” begin with the example using n = input(...). Read the input interaction to see what happens when the user presses Return. Then continue to the paragraph beginning “Here is a program that turns a number of seconds...” and read the seconds example. Focus especially on the separate roles of the original text input and the converted numeric value.
In some browser-based coding environments, the prompt appears in the output console; in others, it may appear in a small input box. The important behavior is the same: your program waits until a value is entered.
Try this short program:
city = input("Which city are you in? ")
print("You entered:", city)
Run it two or three times with different answers. The code does not need to change; the entered data changes each time.
The important surprise: all keyboard input starts as text
Suppose you type 12 when a program asks how many sessions you completed. It looks like a number to you, but Python receives the characters "1" and "2" as a string.
sessions = input("How many study sessions? ")
print(sessions)
print(type(sessions))
If you enter 4, the type display is:
<class 'str'>
So sessions contains "4", not the integer 4.
This design is necessary because a keyboard only supplies characters. Python cannot always know whether 12 should represent a count, an identification code, part of an address, or a number for a calculation. The programmer chooses how to interpret the text.

The difference matters as soon as you calculate. Compare:
print("4" + "1")
print(4 + 1)
The output is:
41
5
For strings, + joins text together. For integers, + performs addition.
Therefore, this program fails:
sessions = input("How many sessions? ")
tomorrow_sessions = sessions + 1
print(tomorrow_sessions)
If the user enters 4, Python cannot add the integer 1 to the string "4". It reports a TypeError because those are incompatible types for this operation.
The remedy is not to change the arithmetic. It is to convert the input at the moment you collect it.
Watch the first part of this video for a visual walkthrough of the same distinction, then the short section on decimal input.
Watch “Python user input” by Bro Code to see input() pause a running program and to observe why conversion is necessary before arithmetic.
Watch input and integers. Notice the moment where an entered age behaves as text until it is wrapped in int(). Then watch decimal input for the contrast between integer and floating-point conversion. Skip the intervening string-concatenation section; later lessons will give you more tools for formatting output.
Convert whole-number input with int()
An integer is appropriate when the input represents a whole-number count:
- number of study sessions
- pages read
- items purchased
- attempts remaining
- people attending
You can convert a string to an integer with int().
A clear two-step version is:
raw_sessions = input("How many study sessions? ")
sessions = int(raw_sessions)
print("Sessions completed:", sessions)
print(type(sessions))
If you enter 4, raw_sessions is initially the string "4". The call int(raw_sessions) converts it into the integer 4, which is stored in sessions.
Once converted, arithmetic works:
sessions = int(input("How many study sessions? "))
tomorrow_sessions = sessions + 1
print("If you complete one more tomorrow:", tomorrow_sessions)
The compact line may look dense at first:
sessions = int(input("How many study sessions? "))
Read it from the innermost function outward:
input(...)displays the prompt and returns text.int(...)converts that text to an integer.=stores the resulting integer insessions.
Python completes the inner input() call before it can run the outer int() call.
Use a temporary variable when you are learning or debugging:
raw_pages = input("How many pages did you read? ")
pages = int(raw_pages)
print("Raw value:", raw_pages)
print("Numeric value:", pages)
print("Pages tomorrow if you read 10 more:", pages + 10)
Later, once the program is working and the intermediate string is not useful, the one-line form is often preferable.
Convert decimal input with float()
A float, short for floating-point number, is appropriate when the value can include a decimal part:
- hours studied, such as
0.75 - distance, such as
2.5 - temperature, such as
18.6 - a measurement, such as
164.2
Use float() to convert text to this numeric type:
hours = float(input("How many hours did you study? "))
print("Hours studied:", hours)
print(type(hours))
If you enter 0.75, Python stores the float 0.75. You can now calculate with it:
hours = float(input("How many hours did you study? "))
weekly_total = hours * 7
print("At that daily rate, weekly hours would be:", weekly_total)
Choose the converter based on what the value means, not only what you happen to type today.
| Meaning of the input | Appropriate conversion | Valid examples |
|---|---|---|
| A count of separate things | int() | 0, 4, 125 |
| A measurement or quantity that may be fractional | float() | 0.5, 2.75, 18.0 |
| A name, command, or description | no numeric conversion | Sam, start, blue |
A float can accept whole-number-looking text as well:
distance = float("3")
print(distance)
This produces:
3.0
But int() is stricter. It accepts "3" but not "3.0" or "3.5":
whole_number = int("3")
decimal_number = int("3.5")
The second example produces a ValueError: the string does not represent a valid whole integer.
For now, provide input in the format requested by the prompt. In a later module, you will learn to handle invalid input gracefully instead of allowing the program to stop with an error.
Build an interactive study-time calculator
Return to the study-time idea from the previous lesson. Before, the values were fixed in the program. Now the user will supply them.
Create a new file in your browser workspace and enter:
sessions = int(input("How many study sessions? "))
hours_per_session = float(input("Hours in each session? "))
total_hours = sessions * hours_per_session
total_minutes = total_hours * 60
print("Total study hours:", total_hours)
print("Total study minutes:", total_minutes)
Run it with:
How many study sessions? 4
Hours in each session? 0.75
The calculations use numeric values:
4 * 0.75 = 3.0
3.0 * 60 = 180.0
The program should report 3.0 total study hours and 180.0 total study minutes.
Make these controlled changes, running the program after each:
- Enter
3sessions and1.5hours per session. - Change
sessionsso that it usesfloat()instead ofint(), then enter2.5. Notice that Python can calculate it, but half a session probably does not make sense for this program. - Restore
int()forsessions. - Temporarily add these lines after the input statements:
print(type(sessions))
print(type(hours_per_session))
Confirm that the first value is an int and the second is a float. Then remove the diagnostic lines once you have verified them.
This illustrates an important distinction: conversion checks whether text has a valid numeric format, but it does not check whether the number makes sense for the situation. For example, -3 can be converted to an integer, even though negative study sessions are not meaningful. You will later use conditions to make decisions about such values.
A reliable input pattern
When a program needs a numeric value, use this pattern:
variable_name = int(input("Clear prompt: "))
or:
variable_name = float(input("Clear prompt: "))
For example:
pages = int(input("How many pages did you read? "))
temperature = float(input("What is the temperature in degrees? "))
Keep these three questions in mind while writing the line:
- What should the prompt tell the user to enter?
- Is this value a whole-number count or can it have a decimal part?
- Will the program perform arithmetic with it?
If the value is used in arithmetic, convert it before calculating. If it is text such as a person’s name, leave it as a string.
Key takeaways
input() makes a program interactive by displaying a prompt and waiting for keyboard input. Its return value is always a string, even when the user types digits.
To calculate with input:
- Use
int()for whole-number counts. - Use
float()for values that may contain decimals. - Nest the calls when convenient, as in
int(input("How many? ")). - Use clear prompts that state what the program needs.
- Expect invalid numeric text to cause an error for now; later you will handle these situations deliberately.
Next, you will begin working more effectively with text by placing values inside readable messages using f-strings.
Can't find a good explanation? Sign up and we'll make it for you
Sign up