Calculator - Python Programming Language

Here is a simple Python program that implements a calculator with the basic arithmetic operations: addition, subtraction, multiplication, and division:


This program uses a while loop to repeatedly prompt the user to select an operation and enter two numbers. It then performs the selected operation on the entered numbers and prints the result. The program exits when the user selects the "Exit" option (choice 5).

Here's a breakdown of how the program works:

  1. The program starts with a function named calculator which contains the entire program logic.

  2. The first thing the program does is initialize an infinite loop with the while keyword. This allows the program to keep running until the user chooses to exit.

  3. Inside the while loop, the program prints a menu of operations the user can choose from, namely: addition, subtraction, multiplication, division, and exit. The menu is printed using several print statements.

  4. Next, the program prompts the user to enter their choice of operation using the input function. The user's input is stored in the variable choice.

  5. After the user has made a choice, the program prompts the user to enter two numbers, num1 and num2 using input() function. These numbers will be used for the selected operation.

  6. Then the program uses if-elif-else statement to check the value of choice variable.

    • If choice is 1, the program will add num1 and num2, and prints the result using a print() statement.
    • If choice is 2, the program will subtract num2 from num1, and prints the result.
    • If choice is 3, the program will multiply num1 and num2, and prints the result.
    • If choice is 4, the program will divide num1 by num2, and prints the result. But it also have a check if num2 is zero or not. Since dividing by zero is not possible.
    • If choice is 5, the program will exit.
  7. If the user enters an invalid choice, the program will print "Invalid Input"

  8. The while loop repeats from the start until the user chooses to exit by entering 5.

  9. Once the loop is exited the program ends.

This program uses simple mathematical operation and control flow statements like if-elif-else and a while loop to create a basic calculator program in Python. While this program can perform basic calculations, it's not a polished user-friendly calculator. You could improve the program by adding more functionalities, handling edge cases, improve the user interface and error handling.

This is a basic example, you could include more functionality and improve the user interface.

Please let me know if you have any questions or if there is any other way I can help.

Program code file is as follows for spider software:

def calculator(): while True: print("Select operaiton.") print("1. Add") print("2. Subtract") print("3. Multiply") print("4. Divide") print("5. Exist") choice = input("Enter choice (1/2/3/4/5): ") num1 = float(input("Enter first number ")) num2 = float(input("Enter second number ")) if choice =='1': print(num1,"+",num2,"=",num1 + num2) elif choice == '2': print(num1,"-",num2,"=",num1 - num2) elif choice =='3': print(num1,"*",num2,"=",num1 * num2) elif choice =='4': if num2 ==0: print("cannot be dividey by 0") else: print(num1,"/",num2,"=",num1/num2) elif choice == 5: break else: print("invalid input") calculator()

After Running the program the output is as follows:

No comments:

Post a Comment

Please disable your ad blocker to support this website.

Our website relies on revenue from ads to keep providing free content.