Day 2 — Input & Calculations
Today, you will learn how to ask users for information, convert their answers into numbers, calculate results, and display them clearly.
Your project is a purchase cost calculator.
Your 180-minute study plan
| Activity | Time | What you will do |
|---|---|---|
| Review | 20 minutes | Practise variables, print, and data types |
| Lesson | 40 minutes | Learn input, conversions, operators, and f-strings |
| Guided exercises | 50 minutes | Complete five exercises |
| Mini-project | 50 minutes | Build your purchase cost calculator |
| Review and self-check | 20 minutes | Test your program and explain your code |
| Total | 180 minutes | Breaks are separate |
Review — 20 minutes
Spend 5 minutes opening IDLE, creating a new file, and saving it as day02.py. Use Run → Run Module to execute your code.
Spend 10 minutes writing a program without copying yesterday’s example:
- Create
namewith a text value. - Create
agewith an integer value. - Create
study_hourswith a decimal value. - Print each value with a label.
- Change one value and run the program again.
Spend 5 minutes explaining the difference between 25 and "25".
Remember: str means text, int means an integer, and float means a floating-point number, such as 2.5.
Lesson — 40 minutes
Spend 10 minutes on each topic below. Type the examples yourself, predict the output, run them, and change a value.
1. Get information with input
Input is information a user gives your program. Output is information your program displays.
name = input("What is your name? ")
print("Hello,", name)
Line by line:
- Line 1: Displays the question and waits. When the user types an answer and presses Enter, the answer is assigned to
name. - Line 2: Displays
Hello,followed by the value ofname.
Example interaction:
What is your name? Ali
Hello, Ali
input always returns a string—even when the user types a number.
Change the example to ask for a city instead of a name.
2. Convert text into numbers
Type conversion means changing a value from one data type to another.
| Tool | Purpose | Example |
|---|---|---|
int(...) | Convert suitable input into an integer | "12" becomes 12 |
float(...) | Convert suitable input into a floating-point number | "2.5" becomes 2.5 |
str(...) | Convert a value into text | 12 becomes "12" |
Example:
age_text = input("Enter your age: ")
age = int(age_text)
print("Age:", age)
Line by line:
- Line 1: Gets the answer as text.
- Line 2: Converts that text into an integer and assigns it to
age. - Line 3: Displays the numeric value with a label.
For a decimal value:
hours_text = input("Enter study hours: ")
hours = float(hours_text)
print("Study hours:", hours)
Line by line:
- Line 1: Gets the study hours as text.
- Line 2: Converts the text into a floating-point number.
- Line 3: Displays the result.
You can also combine the first two steps:
hours = float(input("Enter study hours: "))
Python first runs the inner part, input(...), then converts the answer with float(...).
Use inputs such as 3 for int and 2.5 for float. Use a decimal point, not a comma. int("3.5") cannot convert that text directly. We will learn how to handle invalid input on Day 8.
3. Calculate with operators
An operator is a symbol that performs an operation.
| Operator | Meaning | Example | Result |
|---|---|---|---|
+ | Addition | 8 + 2 | 10 |
- | Subtraction | 8 - 2 | 6 |
* | Multiplication | 8 * 2 | 16 |
/ | Division | 8 / 2 | 4.0 |
Example:
morning_hours = 1.5
evening_hours = 2.0
total_hours = morning_hours + evening_hours
print("Total hours:", total_hours)
Line by line:
- Line 1: Sets the morning study hours.
- Line 2: Sets the evening study hours.
- Line 3: Adds the two values and assigns the result to
total_hours. - Line 4: Displays the total.
Output:
Total hours: 3.5
The + operator behaves differently with strings:
print("10" + "5")
print(10 + 5)
- Line 1: Joins two strings and displays
105. - Line 2: Adds two numbers and displays
15.
This is why you must convert numeric input before calculating.
Multiplication and division happen before addition and subtraction. Parentheses change the order:
| Expression | Result |
|---|---|
2 + 3 * 4 | 14 |
(2 + 3) * 4 | 20 |
4. Display values with f-strings
An f-string, or formatted string, lets you put values inside text.
name = "Sara"
hours = 2.5
print(f"{name} studied for {hours} hours.")
Line by line:
- Line 1: Sets the name.
- Line 2: Sets the study hours.
- Line 3: Places both values inside the sentence and displays it.
Output:
Sara studied for 2.5 hours.
Put f immediately before the opening quotation mark. Put variable names inside braces: {}.
To display two decimal places:
amount = 12.5
print(f"Amount: {amount:.2f}")
- Line 1: Assigns the number to
amount. - Line 2: Displays it as
Amount: 12.50.
The format :.2f means display this number with two digits after the decimal point. It changes the display, not the stored value.
Guided exercises — 50 minutes
Spend 10 minutes on each exercise: 6 minutes writing, 2 minutes checking, and 2 minutes running it with different input. Try before asking for the solution.
Exercise 1 — Say hello
Ask for the user’s name and display a greeting.
Hint: Assign the result of input to a variable.
Exercise 2 — Count books
Ask how many books the user has. Convert the answer into an integer and display it with a label.
Hint: Use int to convert the text.
Exercise 3 — Add study hours
Ask for morning and evening study hours. Calculate and display the total.
Hint: Both inputs may contain decimal values.
Exercise 4 — Calculate an average
Ask for two grades. Calculate and display their average.
Hint: Add the grades first, then divide by two. Think about parentheses.
Exercise 5 — Build a sentence
Ask for the user’s city and commute duration in hours. Display both values in one sentence using an f-string. Show the duration with two decimal places.
Hint: The city is text; the duration is a number.
Mini-project — 50 minutes
Create a new file called purchase_cost.py.
Your program must ask for:
| Information | Suggested variable | Type |
|---|---|---|
| Product name | product_name | str |
| Quantity | quantity | int |
| Unit price | unit_price | float |
| Shipping cost | shipping_cost | float |
Calculate:
Product cost = quantity × unit price
Final cost = product cost + shipping cost
Display the product name, quantity, product cost, shipping cost, and final cost. Use f-strings and show all amounts with two decimal places.
| Time | Task |
|---|---|
| 10 minutes | Plan your inputs, types, formulas, and output |
| 20 minutes | Write the program |
| 10 minutes | Run the two checks below and fix mistakes |
| 10 minutes | Reopen the saved file, run it again, and explain each line |
Check your calculations against these expected results:
| Quantity | Unit price | Shipping | Product cost | Final cost |
|---|---|---|---|---|
2 | 100 | 30 | 200.00 | 230.00 |
3 | 12.5 | 0 | 37.50 | 37.50 |
For today, enter valid numbers. Input validation will come later.
Review and self-check — 20 minutes
Spend 5 minutes explaining input, type conversion, operators, and f-strings in your own words.
Spend 10 minutes answering these questions—two minutes each:
- What type does
inputreturn when the user enters7? - Which conversion should you use for a price of
19.75? - How does
"4" + "6"differ from4 + 6? - What do
fand{}do in an f-string? - Why are parentheses useful when calculating an average?
Spend 5 minutes running your project with new values and checking that the result changes correctly.
Success criteria
- Your program receives all four inputs.
- Quantity uses
int; prices and shipping usefloat. - Both calculation checks produce the expected results.
- Amounts display with two decimal places.
- You can explain why numeric inputs need conversion.
- You answer at least four self-check questions correctly.
