Skip to content

Commit e606634

Browse files
committed
add game calc and logic game
1 parent 2cd4290 commit e606634

File tree

4 files changed

+45
-0
lines changed

4 files changed

+45
-0
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,6 @@
99

1010
### recording game gcd
1111
[![asciicast](https://asciinema.org/a/gOo07GHNNHydhoDA2jznP24Bn.svg)](https://asciinema.org/a/gOo07GHNNHydhoDA2jznP24Bn)
12+
13+
### recording game calc
14+
[![asciicast](https://asciinema.org/a/raMyQxWMsF3ZTEAOgbBHAi96V.svg)](https://asciinema.org/a/raMyQxWMsF3ZTEAOgbBHAi96V)

brain_games/game_logic.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
def start_game(description, question_func):
2+
print("Welcome to the Brain Games!")
3+
name = input("May I have your name? ").strip()
4+
print(f"Hello, {name}!")
5+
print(description)
6+
7+
rounds_to_win = 3
8+
for _ in range(rounds_to_win):
9+
question, correct_answer = question_func()
10+
print(f"Question: {question}")
11+
user_answer = input("Your answer: ").strip()
12+
13+
if user_answer != str(correct_answer):
14+
print(f"'{user_answer}' is wrong answer ;(. Correct answer was '{correct_answer}'.")
15+
print(f"Let's try again, {name}!")
16+
return
17+
18+
print("Correct!")
19+
20+
print(f"Congratulations, {name}!")

brain_games/scripts/brain_calc.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import random
2+
from brain_games.game_logic import start_game
3+
4+
def generate_calculation():
5+
operations = ['+', '-', '*']
6+
num1 = random.randint(1, 100)
7+
num2 = random.randint(1, 100)
8+
operation = random.choice(operations)
9+
question = f"{num1} {operation} {num2}"
10+
11+
if operation == '+':
12+
correct_answer = num1 + num2
13+
elif operation == '-':
14+
correct_answer = num1 - num2
15+
else:
16+
correct_answer = num1 * num2
17+
18+
return question, correct_answer
19+
20+
def main():
21+
start_game("What is the result of the expression?", generate_calculation)

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,4 @@ select = ["E", "F", "I", "C90"]
3535
brain-games = "brain_games.scripts.brain_games:main"
3636
brain-even = "brain_games.scripts.brain_even:main"
3737
brain-gcd = "brain_games.scripts.brain_gcd:main"
38+
brain-calc = "brain_games.scripts.brain_calc:main"

0 commit comments

Comments
 (0)