Repository navigation
Expand file tree
/
Copy pathsimple_calculator.py
More file actions
57 lines (46 loc) · 1.81 KB
/
Copy pathsimple_calculator.py
File metadata and controls
57 lines (46 loc) · 1.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# Simple Calculator Program
# This program allows the user to perform basic arithmetic operations (+, -, *, /, **, //)
# between two numbers entered via the terminal. The user selects the operation, and the
# program displays the result. Division by zero is handled gracefully.
# Note: All inputs are treated as integers.
# Prompt the user to enter the first number
x = input("Enter first number: ")
# Display the list of valid operators to the user
print("""The valid operators are as follows
+ => addition
- => subtraction
* => multiplication
/ => division
** => exponential
// => integer division""")
# Prompt the user to enter the desired operator
operator = input("Enter operator: ")
# Prompt the user to enter the second number
y = input("Enter second number: ")
# Perform addition if operator is '+'
if (operator == "+"):
print(f"{x} + {y} = {int(x) + int(y)}")
# Perform subtraction if operator is '-'
elif (operator == "-"):
print(f"{x} - {y} = {int(x) - int(y)}")
# Perform multiplication if operator is '*'
elif (operator == "*"):
print(f"{x} * {y} = {int(x) * int(y)}")
# Perform division if operator is '/' and handle division by zero
elif (operator == "/"):
if (int(y) != 0):
print(f"{x} / {y} = {int(x) / int(y)}")
else:
print("Division by zero is not possible")
# Perform exponentiation if operator is '**'
elif (operator == "**"):
print(f"{x} ** {y} = {int(x) ** int(y)}")
# Perform integer division if operator is '//' and handle division by zero
elif (operator == "//"):
if (int(y) != 0):
print(f"{x} // {y} = {int(x) // int(y)}")
else:
print("Division by zero is not possible")
# Handle invalid operator input
else:
print("You have entered an invalid input")