forked from scaviangit/simplecalculator-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalculator.py
More file actions
48 lines (41 loc) · 1.01 KB
/
Copy pathcalculator.py
File metadata and controls
48 lines (41 loc) · 1.01 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
import os
from dotenv import load_dotenv
cogs_list = [
'addition',
'subtraction',
'multiplication',
'division'
]
print("Welcome to my simple calculator!")
while True:
try:
a = float(input("Please enter the first number: "))
break
except ValueError:
print("Invalid input. Tip: use numbers only")
while True:
c = input("Select an operation (+ - * /): ").strip()
if c in "+-*/":
break
else:
print("Invalid operation. Tip: choose one of + - * /")
while True:
try:
b = float(input("Great! Now enter the second number: "))
break
except ValueError:
print("Invalid input. Tip: use numbers only")
def calculate(x, y, op):
if op == "+":
return x + y
if op == "-":
return x - y
if op == "*":
return x * y
if op == "/":
if y == 0:
return "Error: division by zero"
return x / y
return "Invalid operator"
result = calculate(a, b, c)
print("Result:", result)