Skip to content

Commit f9f9c87

Browse files
add brain-calc
1 parent c62ed35 commit f9f9c87

File tree

7 files changed

+99
-46
lines changed

7 files changed

+99
-46
lines changed

brain_games/cli.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
11
import prompt
22

33

4-
def welcome_user():
4+
def welcome_user(game_intro=''):
55
print("Welcome to the Brain Games!")
6+
if game_intro:
7+
print(f"{game_intro}")
8+
print()
69
name = prompt.string('May I have your name? ')
710
print(f'Hello, {name}!')
8-
return name
11+
return name
12+
13+
14+
def get_answer(question):
15+
print(f"Question: {question}")
16+
answer = prompt.string("Your answer: ")
17+
return answer

brain_games/game_skeleton.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
from brain_games.cli import get_answer, welcome_user
2+
3+
round = 3
4+
5+
6+
def game_start(game):
7+
player = welcome_user(game.RULES)
8+
9+
total = 0
10+
11+
while total < round:
12+
13+
question, correct = game.start_game()
14+
15+
answer = get_answer(question)
16+
17+
if answer != correct:
18+
print(f"{answer} is wrong answer ;(. Correct answer was {correct}."
19+
f"\nLet's try again, {player}!")
20+
return
21+
print('Correct')
22+
total += 1
23+
24+
print(f'Congratulations, {player}!')
25+
26+
27+
28+
29+
30+
31+

brain_games/games/calc.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from operator import add, mul, sub
2+
from random import choice, randint
3+
4+
RULES = 'What is the result of the expression?'
5+
6+
OPERATIONS = [("+", add), ("-", sub), ("*", mul)]
7+
8+
9+
def start_game():
10+
num1, num2 = randint(1, 100), randint(1, 100)
11+
12+
operator, func = choice(OPERATIONS)
13+
question = f'{num1} {operator} {num2}'
14+
correct = func(num1, num2)
15+
16+
return (question, str(correct))
17+
18+
19+
20+
21+
22+

brain_games/games/even.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from random import randint
2+
3+
RULES = 'Answer "yes" if number even otherwise answer "no".'
4+
5+
6+
def start_game():
7+
answer = randint(1, 100)
8+
9+
if answer % 2 == 0:
10+
is_even = 'yes'
11+
else:
12+
is_even = 'no'
13+
14+
return (answer, is_even)

brain_games/scripts/brain_calc.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from brain_games.game_skeleton import game_start
2+
from brain_games.games import calc
3+
4+
5+
def main():
6+
game_start(calc)
7+
8+
9+
if __name__ == "__main__":
10+
main()
11+
12+
13+
14+
15+
16+

brain_games/scripts/brain_even.py

Lines changed: 4 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,10 @@
1-
from random import randint
2-
3-
from brain_games.cli import welcome_user
1+
from brain_games.game_skeleton import game_start
2+
from brain_games.games import even
43

54

65
def main():
7-
name = welcome_user()
8-
9-
flag = True
10-
print('Answer "yes" if the number is even, otherwise answer "no".')
11-
n = 0
12-
while n != 3:
13-
num = randint(1, 100)
14-
if num % 2 == 0:
15-
flag = True
16-
else:
17-
flag = False
18-
19-
print(f'Question: {num}')
20-
answer = input('Your answer: ')
21-
if answer == 'yes' and flag:
22-
print('Correct!')
23-
n += 1
24-
elif answer == 'no' and not flag:
25-
print('Correct!')
26-
n += 1
27-
else:
28-
if answer == 'yes':
29-
print("'yes' is wrong answer ;(. Correct answer was 'no'."
30-
f"\nLet's try again, {name}!")
31-
break
32-
elif answer == 'no':
33-
print("'no' is wrong answer ;(. Correct answer was 'yes'."
34-
f"\nLet's try again, {name}!")
35-
break
36-
else:
37-
print("use only 'yes' or 'no'."
38-
f"\nLet's try again, {name}!")
39-
break
40-
41-
if n == 3:
42-
print(f'Congratulations, {name}!')
6+
game_start(even)
437

448

459
if __name__ == "__main__":
46-
main()
47-
48-
49-
50-
10+
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)