If you have any query feel free to chat us!
Happy Coding! Happy Learning!
Below is a simple calculator program in Python that can perform basic arithmetic operations: addition, subtraction, multiplication, and division.
pythonCopy code
def add(x, y): return x + y def subtract(x, y): return x - y def multiply(x, y): return x * y def divide(x, y): if y == 0: return "Cannot divide by zero!" return x / y def calculator(): 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 the first number: ")) num2 = float(input("Enter the 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") calculator()
When you run this program, it will display a menu of operations (addition, subtraction, multiplication, and division). You can then enter the choice corresponding to the operation you want to perform, followed by entering the two numbers on which you want to perform the selected operation. The program will then print the result of the operation.
Note: The program uses the
float()
function to convert user input to floating-point numbers to allow decimal values in the input. The division function handles the case of division by zero to avoid raising an error.Please keep in mind that this is a simple calculator program and may not handle all edge cases or complex expressions. You can further extend this program to include more functionality or handle additional operations as needed.
Comments: 0