User Input & Type Casting
Read user input and convert between data types. Read the lesson first, then move through the exercises in order.
After reading
Practice Arena
Begin with the first exercise, then continue step by step through the module.
Start with Greeting by NameStudy Material
Read the full lesson
Why user input matters
So far, a program can run with values that you wrote into the code yourself. That is useful for practice, but real programs become interesting when they react to the user.
A calculator asks for numbers. A login screen asks for a username. A weather app asks for a city.
To do that in Python, you use input().
The input() function
input() pauses the program and waits for the user to type something.
pythonname = input("What is your name? ") print("Hello,", name)
The text inside input() is called the prompt. It tells the user what to enter.
When the user presses Enter, Python gives that typed value back to your program.
input() always returns text
This is one of the most important things in the whole module:
input() always returns a string.
Even if the user types 25, Python receives it as text, not as a number.
pythonage = input("Age: ") print(type(age))
The result is str.
This is why beginners often get confused when they try to do math with input.
Why type casting is needed
If Python reads "25" as text, then it cannot safely treat it as a number until you convert it.
That conversion is called type casting.
The most common casting functions in this module are:
int()for whole numbersfloat()for decimal numbersstr()for text
pythonage = int(input("Age: ")) price = float(input("Price: "))
Now those values can be used in calculations.
int() and float()
Use int() when you expect a whole number.
pythoncount = int(input("How many books? "))
Use float() when decimals are possible.
pythontemperature = float(input("Temperature: "))
This difference matters because some real values, such as money, height, and temperature, often include decimals.
A common beginner problem
Look at this example:
pythonage = input("Age: ") print(age + 1)
This fails because age is text and 1 is a number.
Python does not guess. It expects you to be clear.
The fix is to convert first.
pythonage = int(input("Age: ")) print(age + 1)
Reading more than one value
Many programs need more than one piece of information.
pythonweight = float(input("Weight in kg: ")) height = float(input("Height in meters: "))
You can call input() as many times as needed. Each call asks for one value.
This is how small tools like calculators, converters, and forms work.
Using input in formulas
Once the input is converted to the correct type, you can use it in calculations.
pythoncelsius = float(input("Celsius: ")) fahrenheit = (celsius * 9 / 5) + 32 print(fahrenheit)
The same idea works for BMI, tips, totals, and many other beginner projects.
The flow is usually:
- read input
- convert input
- calculate
- print the result
Formatting the final result
After calculating a value, you often want to show it clearly.
pythonbill = float(input("Bill: ")) tip_percent = int(input("Tip %: ")) total = bill * (1 + tip_percent / 100) print(f"Total: {total:.2f}")
The :.2f part means "show two digits after the decimal point." That is useful for money.
You do not need to master formatting yet, but you should recognize it when you see it.
Invalid input
If you try to convert text that is not a valid number, Python raises an error.
pythonage = int("hello")
This causes a ValueError.
For now, the important thing is to understand why it happens: Python cannot turn the word hello into a numeric value.
Later, you will learn how to handle bad input more safely.
Good habits with input
A few simple habits make beginner code much better:
- write clear prompts
- convert input as soon as needed
- choose variable names that explain the meaning
- keep the steps simple: input, convert, calculate, print
These habits make programs easier to read and easier to fix.
What this lesson should give you
After this lesson, you should understand how to:
- ask the user for input
- remember that input starts as text
- convert text to integers or floats
- use converted values in formulas
- read more than one input in the same program
- print clean final results
That is the core of interactive beginner programs.
Interactive
Exercises for this topic
These exercises follow the exact order of the lesson. Move step-by-step from reading into coding.
Greeting by Name
Complete the "Greeting by Name" exercise.
Dog Years Calculator
Complete the "Dog Years Calculator" exercise.
Celsius to Fahrenheit
Complete the "Celsius to Fahrenheit" exercise.
BMI Calculator
Complete the "BMI Calculator" exercise.
Tip Calculator
Complete the "Tip Calculator" exercise.