Simple Calculator

Perform basic arithmetic operations.

BeginnerPhase 6 - Category 5: Real-World Logic ProblemsExample 11 of 15
simple-calculator.py
1# Take inputs
2num1 = float(input("Enter first number: "))
3operator = input("Enter operator (+, -, *, /): ")
4num2 = float(input("Enter second number: "))
5
6# Calculate
7if operator == '+':
8 result = num1 + num2
9elif operator == '-':
10 result = num1 - num2
11elif operator == '*':
12 result = num1 * num2
13elif operator == '/':
14 if num2 != 0:
15 result = num1 / num2
16 else:
17 result = "Error: Division by zero"
18else:
19 result = "Error: Invalid operator"
20
21print(f"Result: {result}")

Output

Enter first number: 10
Enter operator (+, -, *, /): +
Enter second number: 5
Result: 15.0

What's going on

Use conditionals for different operations.

Key Concepts:

Check operator type
Perform corresponding operation
Handle division by zero