Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
**/*.pyc
**/.DS_Store
.coverage
venv/
venv/
36 changes: 24 additions & 12 deletions src/calculator.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,24 @@
#
# Write a program that given two numbers as input make the main operations.
#
# Output:
# Insert first number: 4
# Insert second number: 2
#
# SUM: 6
# Difference: 2
# Multiplication: 8
# Division: 2
#
def add(a: int, b: int) -> int:
return a + b


def sub(a: int, b: int) -> int:
return a - b


def mul(a: int, b: int) -> int:
return a * b


def div(a: int, b: int) -> float | str:
return "You can't divide by 0" if b == 0 else a / b


if __name__ == "__main__":
x = int(input("Insert first number: "))
y = int(input("Insert second number: "))

print(f"Sum of {x} and {y} = {add(x, y)}")
print(f"Subtraction of {x} and {y} = {sub(x, y)}")
print(f"Multiplication of {x} and {y} = {mul(x, y)}")
print(f"Division of {x} and {y} = {div(x, y)}")
25 changes: 25 additions & 0 deletions tests/test_calculator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from src.calculator import add, div, mul, sub


def test_add() -> None:
assert add(5, 4) == 9
assert add(-1, 1) == 0
assert add(0, 0) == 0


def test_sub() -> None:
assert sub(1, 1) == 0
assert sub(0, 5) == -5


def test_mul() -> None:
assert mul(5, 1) == 5
assert mul(0, 5) == 0
assert mul(-2, 3) == -6


def test_div() -> None:
assert div(5, 1) == 5
assert div(0, 5) == 0
assert div(5, 0) == "You can't divide by 0"
assert div(-6, 2) == -3