File tree Expand file tree Collapse file tree 3 files changed +38
-0
lines changed Expand file tree Collapse file tree 3 files changed +38
-0
lines changed Original file line number Diff line number Diff line change 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
Original file line number Diff line number Diff line change 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 ()
Original file line number Diff line number Diff line change @@ -23,3 +23,4 @@ dev = [
2323[project .scripts ]
2424brain-games = " brain_games.scripts.brain_games:main"
2525brain-even = " brain_games.scripts.brain_even:main"
26+ brain-calc = " brain_games.scripts.brain_calc:main"
You can’t perform that action at this time.
0 commit comments