Siddant Kittur's tools
-
**Calculator Program in Python** ================================ ### Description This program creates a simple calculator that takes two numbers and an operator as input, then performs the specified operation. ### Code ```python def calculator(num1, operator, num2): """ Performs basic arithmetic operations on two numbers. Args: num1 (float): The first number. operator (str): The operator to use for the operation. num2 (float): The second number. Returns: float: The result of the operation. str: An error message if the operator is invalid. """ # Define a dictionary to map operators to their corresponding functions operations = { '+': lambda x, y: x + y, '-': lambda x, y: x - y, '*': lambda x, y: x * y, '/': lambda x, y: x / y if y != 0 else 'Error: Division by zero' } # Check if the operator is valid if operator in operations: # Perform the operation result = operations[operator](num1, num2) return result else: return 'Invalid Operator' def main(): # Get user input try: num1 = float(input("Enter the first number: ")) operator = input("Enter the operator (+, -, *, /): ") num2 = float(input("Enter the second number: ")) # Calculate and print the result result = calculator(num1, operator, num2) print(f"{num1} {operator} {num2} = {result}") except ValueError: print("Invalid input. Please enter a number.") if __name__ == "__main__": main() ``` ### Explanation * The `calculator` function takes two numbers and an operator as input. * It uses a dictionary to map operators to their corresponding lambda functions. * The function checks if the operator is valid and performs the operation if it is. * If the operator is invalid, it returns an 'Invalid Operator' message. * The `main` function gets user input, calls the `calculator` function, and prints the result. ### Running the Code 1. Save the code in a file with a `.py` extension (e.g., `calculator.py`). 2. Open a terminal or command prompt and navigate to the directory where you saved the file. 3. Run the code using Python (e.g., `python calculator.py`). 4. Follow the prompts to enter the numbers and operator. ### Example Usage ``` Enter the first number: 10 Enter the operator (+, -, *, /): + Enter the second number: 5 10.0 + 5.0 = 15.0 ``` ``` Enter the first number: 10 Enter the operator (+, -, *, /): ^ Enter the second number: 5 10.0 ^ 5.0 = Invalid Operator ```
-
Smart AI finance manager that auto-categorizes spending, builds personalized budgets, tracks goals.Open
-

-

-
Siddant Kittur๐ ๏ธ 7 tools ๐ 156 karmaJul 27, 2025@Ilustration Generatorlet people know what ilustration generator is to explore -

-
AI-powered Instagram comments that feel authentic and boost engagement.Open
Comments
On Ilustration Generator
Siddant Kittur
๐ ๏ธ 7 tools
๐ 156 karma
Jul 27, 2025
@Ilustration Generator
let people know what ilustration generator is to explore
