Calculator Program In Python

Dear Sciaku Learner you are not logged in or not enrolled in this course.

Please Click on login or enroll now button.

If you have any query feel free to chat us!

Happy Coding! Happy Learning!

Lecture 36:- Calculator Program In Python

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.

5. Flow Control

Comments: 0

Frequently Asked Questions (FAQs)

How do I register on Sciaku.com?
How can I enroll in a course on Sciaku.com?
Are there free courses available on Sciaku.com?
How do I purchase a paid course on Sciaku.com?
What payment methods are accepted on Sciaku.com?
How will I access the course content after purchasing a course?
How long do I have access to a purchased course on Sciaku.com?
How do I contact the admin for assistance or support?
Can I get a refund for a course I've purchased?
How does the admin grant access to a course after payment?