TAAFT
Free mode
100% free
Freemium
Free Trial
Deals

Siddant Kittur's tools

  • Free Code Python Generator
    Generate clean Python code from simple descriptions.
    Open
    **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 ```
  • AI Personal Finance manager
    Smart AI finance manager that auto-categorizes spending, builds personalized budgets, tracks goals.
    Open
    AI Personal Finance manager website
  • futuredude
    AI career coach that crafts perfect job matches.
    Open
    futuredude website
  • Linux Command Generator
    Turn plain English into Linux commands instantly.
    Open
    Linux Command Generator website
  • Ilustration Generator
    Describe your vision, get stunning digital illustrations.
    Open
    Ilustration Generator website
  • Website And App Generator
    Transform ideas into complete website blueprints.
    Open
    Website And App Generator website
  • Instagram Coments Generator
    AI-powered Instagram comments that feel authentic and boost engagement.
    Open
    Instagram Coments Generator website

Comments

Siddant Kittur
๐Ÿ› ๏ธ 7 tools ๐Ÿ™ 156 karma
Rated it
let people know what ilustration generator is to explore
0 AIs selected
Clear selection
#
Name
Task