-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
72 lines (59 loc) · 1.6 KB
/
main.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
def add(a, b):
"""
Returns the sum of a and b.
"""
return a + b
def subtract(a, b):
"""
Returns the difference of a and b.
"""
return a - b
def multiply(a, b):
"""
Returns the product of a and b.
"""
return a * b
def divide(a, b):
"""
Returns the quotient of a divided by b.
Handles division by zero.
"""
try:
return a / b
except ZeroDivisionError:
return "Error: Division by zero is not allowed."
def calculate(a, operator, b):
"""
Calls the appropriate arithmetic function based on the operator.
"""
if operator == '+':
return add(a, b)
elif operator == '-':
return subtract(a, b)
elif operator == '*':
return multiply(a, b)
elif operator == '/':
return divide(a, b)
else:
return "Invalid operator. Please use '+', '-', '*', or '/'."
def main():
"""
Main function to run the calculator.
"""
print("Welcome to the Basic Calculator!\n")
while True:
try:
a = float(input("Enter the first number (or 'q' to quit): ").strip())
except ValueError:
print("Exiting the calculator. Goodbye!")
break
operator = input("Enter an operator (+, -, *, /): ").strip()
try:
b = float(input("Enter the second number: ").strip())
except ValueError:
print("Invalid input. Please enter a valid number.\n")
continue
result = calculate(a, operator, b)
print(f"Result: {a} {operator} {b} = {result}\n")
if __name__ == "__main__":
main()