-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChain calc.py
45 lines (42 loc) · 1.4 KB
/
Chain calc.py
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
def calculator():
result = None
while True:
if result is None:
try:
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
except ValueError:
print("Invalid input. Please enter numbers only.")
continue
else:
num1 = result
try:
num2 = float(input("Enter next number: "))
except ValueError:
print("Invalid input. Please enter a number.")
continue
choice = input("Choose operator (+, -, *, /) or 'c' to clear or 'q' to quit: ")
if choice == "+":
result = num1 + num2
print("Result:", result)
elif choice == "-":
result = num1 - num2
print("Result:", result)
elif choice == "*":
result = num1 * num2
print("Result:", result)
elif choice == "/":
if num2 == 0:
print("Cannot divide by zero.")
continue
result = num1 / num2
print("Result:", result)
elif choice == 'c':
result = None
print("Calculator cleared.")
elif choice == 'q':
print("Exiting calculator.")
break
else:
print("Invalid choice. Please enter +, -, *, /, c, or q.")
calculator()