-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCalculator_task.py
More file actions
48 lines (44 loc) · 1.61 KB
/
Calculator_task.py
File metadata and controls
48 lines (44 loc) · 1.61 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
def calculator():
print("Calculator")
print("-----------------")
print("Operations available:")
print("1. Addition (+)")
print("2. Subtraction (-)")
print("3. Multiplication (*)")
print("4. Division (/)")
print("5. Modulus (%)")
print("6. Exponentiation (**)")
try:
# Getting user input
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
operation = input("Enter operation (+, -, *, /, %, **): ").strip()
# Perform calculation based on operation
if operation == '+':
result = num1 + num2
print(f"Result: {num1} + {num2} = {result}")
elif operation == '-':
result = num1 - num2
print(f"Result: {num1} - {num2} = {result}")
elif operation == '*':
result = num1 * num2
print(f"Result: {num1} * {num2} = {result}")
elif operation == '/':
if num2 == 0:
print("Error: Division by zero is not allowed!")
else:
result = num1 / num2
print(f"Result: {num1} / {num2} = {result}")
elif operation == '%':
result = num1 % num2
print(f"Result: {num1} % {num2} = {result}")
elif operation == '**':
result = num1 ** num2
print(f"Result: {num1} ** {num2} = {result}")
else:
print("Invalid operation entered!")
except ValueError:
print("Error: Please enter valid numbers!")
except Exception as e:
print(f"An error occurred: {e}")
calculator()