calculater in python

            You can create a simple calculator in Python using basic arithmetic operations and functions. Here's an example of a basic text-based calculator:


# Simple Calculator




# Function for addition


def add(x, y):


    return x + y




# Function for subtraction


def subtract(x, y):


    return x - y




# Function for multiplication


def multiply(x, y):


    return x * y




# Function for division


def divide(x, y):


    if y == 0:


        return "Error! Division by zero."


    return x / y




# Main Program


print("Select operation:")


print("1. Add")


print("2. Subtract")


print("3. Multiply")


print("4. Divide")




choice = input("Enter choice (1/2/3/4): ")




num1 = float(input("Enter first number: "))


num2 = float(input("Enter second number: "))




if choice == '1':


    print(f"{num1} + {num2} = {add(num1, num2)}")




elif choice == '2':


    print(f"{num1} - {num2} = {subtract(num1, num2)}")




elif choice == '3':


    print(f"{num1} * {num2} = {multiply(num1, num2)}")




elif choice == '4':


    print(f"{num1} / {num2} = {divide(num1, num2)}")




else:


    print("Invalid Input")



    

How it works:

  • The user selects the operation (addition, subtraction, multiplication, or division).
  • The user inputs two numbers.
  • The selected operation is performed, and the result is printed.

Would you like to modify or extend this in any way?

Comments

Post a Comment

Popular posts from this blog

What is the HTML

box office collection pushpa movie

pbks vs rcb