Skip to content

Commit 4eaebd2

Browse files
authored
Merge pull request #11 from xing-zhixu/feature/add-power-modulo
feat: add power and modulo operations + input validation
2 parents fc6a5b9 + 4f45404 commit 4eaebd2

1 file changed

Lines changed: 24 additions & 8 deletions

File tree

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,42 @@
11
def add(x, y):
22
return x + y
33

4-
54
def subtract(x, y):
65
return x - y
76

8-
97
def multiply(x, y):
108
return x * y
119

12-
1310
def divide(x, y):
1411
if y == 0:
1512
return "Cannot divide by zero!"
1613
return x / y
1714

15+
def power(x, y):
16+
return x ** y
17+
18+
def modulo(x, y):
19+
if y == 0:
20+
return "Cannot mod by zero!"
21+
return x % y
1822

1923
def calculator():
2024
print("1. Add")
2125
print("2. Subtract")
2226
print("3. Multiply")
2327
print("4. Divide")
24-
choice = int(input("Enter your choice (1/2/3/4): "))
25-
num1 = float(input("Enter first number: "))
26-
num2 = float(input("Enter second number: "))
28+
print("5. Power")
29+
print("6. Modulo")
30+
31+
32+
try:
33+
choice = int(input("Enter your choice (1/2/3/4/5/6): "))
34+
num1 = float(input("Enter first number: "))
35+
num2 = float(input("Enter second number: "))
36+
except ValueError:
37+
print("Invalid input! Please enter numbers only.")
38+
return
39+
2740
if choice == 1:
2841
print("Result:", add(num1, num2))
2942
elif choice == 2:
@@ -32,8 +45,11 @@ def calculator():
3245
print("Result:", multiply(num1, num2))
3346
elif choice == 4:
3447
print("Result:", divide(num1, num2))
48+
elif choice == 5:
49+
print("Result:", power(num1, num2))
50+
elif choice == 6:
51+
print("Result:", modulo(num1, num2))
3552
else:
3653
print("Invalid choice!")
3754

38-
39-
calculator()
55+
calculator()

0 commit comments

Comments
 (0)