Lesson 10 of 10
Time to put it all together. You'll build a small calculator that:
- Asks the user for two numbers
- Asks which operation to perform
- Prints the result
This uses every concept from Module 1: input(), type conversion, variables, f-strings, and
arithmetic operators.
A quick preview: choosing between options
"Pick an operation" means our program needs to behave differently depending on what the user
typed. That's normally Module 2 territory (if / elif / else), but here's a quick preview
so you can finish the project now — we'll cover this properly, in depth, next module.
operation = "add"
if operation == "add":
print("Adding!")
elif operation == "subtract":
print("Subtracting!")
else:
print("Unknown operation")
Read it like this: "if operation equals \"add\", do this; otherwise if it equals
\"subtract\", do that; otherwise, do this last thing." == checks equality — it's different
from the single = you use for assignment.
Building the calculator
num1 = float(input("First number: "))
num2 = float(input("Second number: "))
operation = input("Operation (+, -, *, /): ")
if operation == "+":
result = num1 + num2
elif operation == "-":
result = num1 - num2
elif operation == "*":
result = num1 * num2
elif operation == "/":
result = num1 / num2
else:
result = "Unknown operation"
print(f"Result: {result}")
Walk through it: two input() calls collect numbers as text and immediately convert them with
float(). A third input() collects the operator as a string. Then an if/elif chain picks
the right arithmetic operator based on what the user typed, and an f-string prints the result.
Try it yourself
Run the calculator below. Try +, -, *, and / to make sure each branch works.
Module 1 complete
You now know how to store data in variables, work with Python's core types, convert between them, format strings, read input, and do arithmetic — and you've shipped a working program. That's the real foundation everything else builds on.
Next up: Module 2 — Control Flow, where if/elif/else and loops get the full treatment.
Questions about this lesson
Log in to ask a question.