Repository navigation
Expand file tree
/
Copy pathcalculator.py
More file actions
67 lines (53 loc) · 1.82 KB
/
Copy pathcalculator.py
File metadata and controls
67 lines (53 loc) · 1.82 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
"""A simple calculator module that provides basic arithmetic operations."""
def add(a, b):
"""Return the sum of a and b."""
return a + b
def subtract(a, b):
"""Return the difference of a and b."""
return a - b
def multiply(a, b):
"""Return the product of a and b."""
return a * b
def divide(a, b):
"""Return the quotient of a and b. Raises ValueError on division by zero."""
if b == 0:
return "Cannot divide by zero."
return a / b
def floor_divide(a, b):
"""Return the floor division of a by b. Raises ValueError on division by zero."""
if b == 0:
return "Cannot divide by zero."
return a // b
def modulus(a, b):
"""Return the modulus of a by b. Raises ValueError on division by zero."""
if b == 0:
raise ValueError("Cannot divide by zero.")
return a % b
while True:
# Ask the user for two numbers
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
# Ask the user for an operation (+, -, *, /, //, %)
operation = input("Enter operation ((+, -, *, /, //, %): ")
# Perform the calculation and display the result
match operation:
case "+":
result = add(num1, num2)
case "-":
result = subtract(num1, num2)
case "*":
result = multiply(num1, num2)
case "/":
result = divide(num1, num2)
case "//":
result = floor_divide(num1, num2)
case "%":
result = modulus(num1, num2)
case _:
result = "Invalid operation"
print(f"Result: {result}")
# Allow the user to perform multiple calculations in a loop
new_operation = input("Do you want to perform another operation? (y/n): ")
if new_operation == "n":
print("Exiting the calculator. Goodbye!")
break