|
| 1 | +Write a program to make a simple calculator. The program should perform four basic operations (1-addition, 2-subtraction, 3-multiplication, and 4-division) |
| 2 | +depending on the user’s choice. |
| 3 | +The program will take two values from the user and the type of operation. The result will be displayed based on the operation chosen by the user. |
| 4 | +The program will be repeated until user requests to stop. |
| 5 | + |
| 6 | + |
| 7 | + |
| 8 | + |
| 9 | +def addition(a, b): |
| 10 | + return a + b |
| 11 | + |
| 12 | +def subtraction(a, b): |
| 13 | + return a - b |
| 14 | + |
| 15 | +def multiplication(a, b): |
| 16 | + return a * b |
| 17 | + |
| 18 | +def division(a, b): |
| 19 | + return a / b |
| 20 | + |
| 21 | + |
| 22 | +print("Select operation.") |
| 23 | +print("1.Addition") |
| 24 | +print("2.Subtraction") |
| 25 | +print("3.Multiplication") |
| 26 | +print("4.Division") |
| 27 | + |
| 28 | +print(' ') |
| 29 | + |
| 30 | +while True: |
| 31 | + operation = input("Choose operation(1/2/3/4): ") |
| 32 | + |
| 33 | + if operation in ('1', '2', '3', '4'): |
| 34 | + num1 = float(input("Enter first number: ")) |
| 35 | + num2 = float(input("Enter second number: ")) |
| 36 | + |
| 37 | + if operation == '1': |
| 38 | + print(num1, "+", num2, "=", addition(num1, num2)) |
| 39 | + |
| 40 | + elif operation == '2': |
| 41 | + print(num1, "-", num2, "=", subtraction(num1, num2)) |
| 42 | + |
| 43 | + elif operation == '3': |
| 44 | + print(num1, "*", num2, "=", multiplication(num1, num2)) |
| 45 | + |
| 46 | + elif operation == '4': |
| 47 | + print(num1, "/", num2, "=", division(num1, num2)) |
| 48 | + break |
| 49 | + else: |
| 50 | + print("You entered the invalid input") |
0 commit comments