Simple Calculator
Perform basic arithmetic operations.
BeginnerPhase 6 - Category 5: Real-World Logic ProblemsExample 11 of 15
simple-calculator.py
1# Take inputs2num1 = float(input("Enter first number: "))3operator = input("Enter operator (+, -, *, /): ")4num2 = float(input("Enter second number: "))56# Calculate7if operator == '+':8 result = num1 + num29elif operator == '-':10 result = num1 - num211elif operator == '*':12 result = num1 * num213elif operator == '/':14 if num2 != 0:15 result = num1 / num216 else:17 result = "Error: Division by zero"18else:19 result = "Error: Invalid operator"2021print(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
🔗Related Content
- 📖
Phase 6 - Learn Concepts
Review Phase 6 concepts and explanations
- 📝
Phase 6 - Quiz
Test your Phase 6 understanding with quiz questions
- 💻
Phase 6 - All Practice Problems
Explore all practice problems for Phase 6
- 🎓
Master Your Logic Building - Complete Course
Browse all phases and tutorials
- 🧠
Logic Building Overview
Learn about the complete logic building curriculum