Phase: 1. Fundamentals | Estimated time: 1.5 hours | Milestone Project: No
By the end of this module, you will be able to:
- Get text input from the user using
input() - Print dynamic output that includes user-provided values
- Convert string input to numeric types for calculation
- Build a simple interactive program
Programs that only display fixed output are boring. The input() function is your gateway to building interactive applications — from simple Q&A scripts to games, calculators, and data-entry tools.
input() pauses the program and waits for the user to type something and press Enter. Whatever the user types is returned as a string:
name = input()
print("Hello,", name)You can pass a prompt string to input() that is displayed to the user:
name = input("What is your name? ")
print("Nice to meet you,", name)input() always returns a string. Even if the user types a number:
age = input("How old are you? ")
print(age, type(age)) # e.g., 25 <class 'str'>If you need a number, you must convert it yourself using int() or float():
age = int(input("How old are you? "))
print("Next year you'll be", age + 1)You can build sentences using f-string style (we'll cover f-strings properly in Module 008) or by passing multiple arguments to print():
name = input("Enter your name: ")
color = input("Enter your favorite color: ")
print(name, "likes", color)A simple calculator using input:
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
print("Sum:", num1 + num2)
print("Difference:", num1 - num2)When running in a terminal, input() waits forever until the user types something. In a Jupyter notebook, you may need to run code cells that simulate input differently (see the notebook).
- Forgetting that
input()returns a string:input() + 5causesTypeError. Convert tointorfloatfirst. - Not stripping whitespace:
input()preserves leading/trailing spaces. Use.strip()to clean input. - Using
input()in an online/notebook environment: Some environments don't support interactive input. Use a simulated approach or pass the prompt carefully. - Assuming the prompt needs a newline:
input()does not add a newline after the prompt. Add a space at the end of your prompt string for readability. - Chaining conversions unsafely:
int(input())raisesValueErrorif the user types non-numeric text. We'll learn error handling for this in a later module.
Let's build a simple "Personal Greeting" program:
# Ask for user's details
name = input("What is your name? ")
age = int(input("How old are you? "))
city = input("Which city do you live in? ")
# Generate a personalized message
print()
print("Hello,", name + "!")
print("You are", age, "years old and live in", city + ".")
print("In 5 years, you will be", age + 5, "years old.")Example run:
What is your name? Alice
How old are you? 30
Which city do you live in? London
Hello, Alice!
You are 30 years old and live in London.
In 5 years, you will be 35 years old.
Now let's build an interest calculator:
principal = float(input("Enter the principal amount: "))
rate = float(input("Enter the interest rate (as %, e.g. 5 for 5%): "))
years = int(input("Number of years: "))
interest = principal * (rate / 100) * years
total = principal + interest
print("Interest earned:", interest)
print("Total after", years, "years:", total)input()reads a line of text from the user as a string.- Always convert numeric input with
int()orfloat()before doing math. input()accepts an optional prompt string.- Combine
input()andprint()for interactive programs. - Use
.strip()to clean whitespace from input. input()returns a string regardless of what the user types.- Prompt strings should end with a space for readability.
- Simple interactive programs follow: input → process → output.
Learn the building blocks of computation in Module 006: Operators.