Here is a simple Python program that implements a calculator with the basic arithmetic operations: addition, subtraction, multiplication, and division:
Here's a breakdown of how the program works:
The program starts with a function named
calculator
which contains the entire program logic.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.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.Next, the program prompts the user to enter their choice of operation using the
input
function. The user's input is stored in the variablechoice
.After the user has made a choice, the program prompts the user to enter two numbers,
num1
andnum2
usinginput()
function. These numbers will be used for the selected operation.Then the program uses
if-elif-else
statement to check the value ofchoice
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.
- If choice is 1, the program will add num1 and num2, and prints the result using a
If the user enters an invalid choice, the program will print "Invalid Input"
The while loop repeats from the start until the user chooses to exit by entering 5.
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