AiTechWorlds
AiTechWorlds
Imagine you walk into a bank to open an account. The clerk asks you a series of questions: "What's your name?" You answer — and she writes it down. "What's your date of birth?" You say "1995" — and she writes the number 1995, not the words "nineteen ninety-five."
That distinction matters. Your name is text. Your birth year is a number. The bank clerk knows the difference. Your Python program needs to know it too.
This lesson is about collecting information from the person using your program — and making sure that information is the right type for what you need to do with it.
input() FunctionPython gives you the input() function to ask a user for information. Here's the simplest version:
name = input("What is your name? ")
print("Hello,", name)
What happens:
What is your name? nameprint("Hello,", name) displays the greetingSample run:
What is your name? Sarah
Hello, Sarah
Simple. But here's where almost every beginner gets tripped up.
input() Always Returns TextLet's say you ask for someone's age:
age = input("How old are you? ")
print("Your age is:", age)
print("Type:", type(age))
Output:
How old are you? 25
Your age is: 25
Type: <class 'str'>
Did you notice? Even though the user typed 25 (a number), Python stored it as '25' (a string — text). input() always returns a string, no matter what the user types.
Why does Python do this? Because Python can't know what you intend to do with the input. Maybe you want it as a number. Maybe you want to check if the input contains certain characters. By giving you a string, Python lets you decide.
Let's try to calculate next year using the age we just collected:
age = input("How old are you? ")
next_year_age = age + 1
print("Next year you'll be:", next_year_age)
What actually happens:
How old are you? 25
TypeError: can only concatenate str (not "int") to str
Python is trying to add the string "25" to the integer 1. You can't add apples to oranges. In Python, you can't add text to a number directly.
This error message is actually very helpful once you know how to read it. It says: can only concatenate str (not "int") to str — meaning it was trying to glue two strings together (concatenate), but found an integer instead.
Python gives you four core type conversion functions:
| Function | What it does | Example | Result |
|---|---|---|---|
int(x) | Converts to integer | int("25") | 25 |
float(x) | Converts to decimal | float("3.14") | 3.14 |
str(x) | Converts to text | str(100) | "100" |
bool(x) | Converts to True/False | bool(0) | False |
Let's fix our age program:
age = input("How old are you? ")
age = int(age) # Convert text "25" to number 25
next_year_age = age + 1
print("Next year you'll be:", next_year_age)
Output:
How old are you? 25
Next year you'll be: 26
You can also write it on one line — many Python developers prefer this:
age = int(input("How old are you? "))
next_year_age = age + 1
print("Next year you'll be:", next_year_age)
The input() runs first, returns the string "25", then int() immediately converts it to 25.
Use float() when you expect a decimal value:
price = float(input("Enter the item price: $"))
quantity = int(input("How many? "))
total = price * quantity
print(f"Total: ${total:.2f}")
Sample run:
Enter the item price: $4.99
How many? 3
Total: $14.97
The :.2f inside the f-string means "show 2 decimal places" — a cleaner way to display money.
What if the user types something unexpected?
age = int(input("Enter your age: "))
What if user types "twenty"?
Enter your age: twenty
ValueError: invalid literal for int() with base 10: 'twenty'
int() can only convert strings that look like numbers. "twenty" doesn't. In a real application, you'd want to handle this gracefully — but for now, just be aware it can happen.
Let's build something genuinely useful. The Body Mass Index (BMI) formula is:
BMI = weight (kg) / height (m)²
This is a real medical measurement. A BMI between 18.5–24.9 is considered healthy by the World Health Organization.
# BMI Calculator
print("=== BMI Calculator ===")
print()
name = input("Enter your name: ")
weight_kg = float(input("Enter your weight in kilograms: "))
height_m = float(input("Enter your height in meters: "))
# Calculate BMI
bmi = weight_kg / (height_m ** 2)
# Determine category
if bmi < 18.5:
category = "Underweight"
elif bmi < 25:
category = "Normal weight"
elif bmi < 30:
category = "Overweight"
else:
category = "Obese"
# Display results
print()
print(f"Hello, {name}!")
print(f"Your BMI is: {bmi:.1f}")
print(f"Category: {category}")
print()
print("BMI Categories (WHO guidelines):")
print(" Under 18.5 → Underweight")
print(" 18.5 - 24.9 → Normal weight")
print(" 25.0 - 29.9 → Overweight")
print(" 30.0 and above → Obese")
Sample run:
=== BMI Calculator ===
Enter your name: Alice
Enter your weight in kilograms: 65
Enter your height in meters: 1.68
Hello, Alice!
Your BMI is: 23.0
Category: Normal weight
BMI Categories (WHO guidelines):
Under 18.5 → Underweight
18.5 - 24.9 → Normal weight
25.0 - 29.9 → Overweight
30.0 and above → Obese
Notice how the program:
# Text input (no conversion needed)
name = input("Name: ")
# Integer input
age = int(input("Age: "))
# Decimal input
price = float(input("Price: "))
# Yes/No input (convert to lowercase for reliable comparison)
answer = input("Continue? (yes/no): ").lower()
# Input with a default message stripped of whitespace
city = input("City: ").strip()
The .lower() and .strip() methods at the end are common patterns you'll see throughout Python code. They make sure user input is in a predictable format before you use it.
Try building this yourself:
Sample output:
Bill amount: $45.00
Tip %: 20
Tip: $9.00
Total: $54.00
Split 2 ways: $27.00 each
Split 3 ways: $18.00 each
Split 4 ways: $13.50 each
Get this course's notes on Telegram!
Free cheat sheets, summaries & practice exercises