-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalculator_V1.py
More file actions
50 lines (33 loc) · 1.06 KB
/
calculator_V1.py
File metadata and controls
50 lines (33 loc) · 1.06 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
# python calculator (Level: Novice)
from colorama import Fore,Style,init
init(autoreset=True)
print(Fore.CYAN+"\n===Simple Calculator===")
try:
num1 = float(input("Enter first number: "))
operator = input("Enter operator (+, -, *, /, %, **): ")
num2 = float(input("Enter second number: "))
if operator == "+":
result = num1 + num2
elif operator == "-":
result = num1 - num2
elif operator == "*":
result = num1 * num2
elif operator == "**":
result = num1 ** num2
elif operator == "/":
if num2 == 0:
print(Fore.RED+"Error: Cannot divide by zero")
else:
result = num1 / num2
elif operator == "%":
if num2 == 0:
print(Fore.RED+"Error: Cannot modulo by zero")
else:
result = num1 % num2
else:
print(Fore.RED+"Error: Invalid operator")
print(Fore.GREEN+f"Result: {result}")
except ValueError:
print(Fore.RED+"Error: Please enter valid numbers")
except Exception:
print(Fore.RED+"Unexpected error:")