Skip to content

Commit 80ef62d

Browse files
committed
Add calc.py, scripts/brain_calc.py, apdate pyproject.toml
1 parent dfa4b0e commit 80ef62d

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed

brain_games/games/calc.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import secrets
2+
import operator
3+
4+
5+
def calculate(num1: int, num2: int, operation: str) -> int:
6+
"""Calculate the result of two numbers with given operation."""
7+
operations = {
8+
'+': operator.add,
9+
'-': operator.sub,
10+
'*': operator.mul
11+
}
12+
return operations[operation](num1, num2)
13+
14+
15+
def generate_round() -> tuple[str, str]:
16+
"""Generate a round for the calculator game."""
17+
num1 = secrets.randbelow(50) + 1 # 1-50
18+
num2 = secrets.randbelow(50) + 1 # 1-50
19+
operation = secrets.choice(['+', '-', '*'])
20+
21+
question = f"{num1} {operation} {num2}"
22+
correct_answer = str(calculate(num1, num2, operation))
23+
24+
return question, correct_answer

brain_games/scripts/brain_calc.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/usr/bin/env python3
2+
from brain_games.engine import run_game
3+
from brain_games.games.calc import generate_round
4+
5+
DESCRIPTION = 'What is the result of the expression?'
6+
7+
8+
def main():
9+
run_game(DESCRIPTION, generate_round)
10+
11+
12+
if __name__ == '__main__':
13+
main()

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,4 @@ dev = [
2323
[project.scripts]
2424
brain-games = "brain_games.scripts.brain_games:main"
2525
brain-even = "brain_games.scripts.brain_even:main"
26+
brain-calc = "brain_games.scripts.brain_calc:main"

0 commit comments

Comments
 (0)