Project 2: Simple Calculator
Singam Ravi Babu
Data Scientist & Analyst | AI Developer | Software Architect | Git | GitHub | GitHub Options | Azure DevOps
Description:
The Simple Calculator project involves creating a basic calculator program that can perform arithmetic operations like addition, subtraction, multiplication, and division based on user input. It's a fundamental project that helps students understand how to take input from users, perform calculations, and display results.
Example:
# Simple Calculator program in Python
# Function to perform addition
def add(x, y):
return x + y
# Function to perform subtraction
def subtract(x, y):
return x - y
# Function to perform multiplication
def multiply(x, y):
return x * y
# Function to perform division
def divide(x, y):
if y != 0:
return x / y
else:
return "Error: Division by zero!"
print("Select operation:")
print("1. Addition")
print("2. Subtraction")
print("3. Multiplication")
print("4. Division")
choice = input("Enter choice (1/2/3/4): ")
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
if choice == '1':
print("Result:", add(num1, num2))
elif choice == '2':
print("Result:", subtract(num1, num2))
elif choice == '3':
print("Result:", multiply(num1, num2))
elif choice == '4':
print("Result:", divide(num1, num2))
else:
print("Invalid choice!")
Explanation:
- The program begins by defining four functions (`add`, subtract, multiply, divide) to perform the four basic arithmetic operations.
- It then presents a menu to the user, prompting them to select the desired operation.
- Based on the user's choice, it takes two numbers as input.
- It then calls the appropriate function to perform the selected operation on the input numbers and displays the result.
Challenges:
Here are five challenges to enhance the Simple Calculator project:
领英推è
1. Advanced Operations:
- Add functionality to the calculator to handle exponentiation (power) and modulus operations.
2. Decimal Precision:
- Modify the program to round the result to a specified number of decimal places.
3. Error Handling:
- Implement error handling to gracefully handle invalid input, such as non-numeric inputs or invalid operations.
4. Memory Feature:
- Extend the calculator to include memory functionality, allowing users to store and recall previous results.
5. Scientific Calculator:
- Transform the calculator into a scientific calculator by incorporating functions like square root, logarithm, and trigonometric functions.