Skip to content

Commit 550781e

Browse files
author
Виталий
committed
First correction accordance with the recommendations
1 parent 6c6e588 commit 550781e

File tree

13 files changed

+129
-86
lines changed

13 files changed

+129
-86
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,6 @@
33
dist/
44
*.pyc
55
hello.py
6+
.idea/
7+
__pycache__
8+
*_cache/

brain_games/engine_game.py

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,21 @@
11
import prompt
22

33

4-
def engine(rule: str, questions: list, right_answer: list):
4+
def run_game(displays_rules_game, generating_question_and_answer):
5+
NUMBER_QUESTIONS = 3
56
print('Welcome to the Brain Games!')
67
name = prompt.string('May I have your name? ')
78
print(f'Hello, {name}!')
8-
print(rule)
9-
num_questions = len(questions)
10-
num_errors = 0
11-
while num_questions > 0:
12-
print(f"Question: {questions[num_questions - 1]}")
9+
displays_rules_game()
10+
for _ in range(NUMBER_QUESTIONS):
11+
question, right_answer = generating_question_and_answer()
12+
print(f"Question: {question}")
1313
response_user = prompt.string('Your answer: ')
14-
if response_user == right_answer[num_questions - 1]:
14+
if response_user == right_answer:
1515
print('Correct!')
16-
num_questions -= 1
1716
else:
1817
print(f"'{response_user}' is wrong answer ;(. ", end='')
19-
print(f"Correct answer was '{right_answer[num_questions - 1]}'.")
18+
print(f"Correct answer was '{right_answer}'.")
2019
print(f"Let's try again, {name}!")
21-
num_questions = 0
22-
num_errors = 1
23-
if num_errors == 0:
24-
print(f"Congratulations, {name}!")
20+
return
21+
print(f"Congratulations, {name}!")

brain_games/games/calc.py

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
11
from random import choice, randint
22

3-
rules = "What is the result of the expression?"
4-
questions = []
5-
right_answer = []
6-
for _ in range(3):
7-
num1 = randint(1, 25)
8-
num2 = randint(1, 25)
3+
4+
def displays_rules_game():
5+
print("What is the result of the expression?")
6+
7+
8+
def generating_question_and_answer():
9+
MIN_GEN_RANGE = 1
10+
MAX_GEN_RANGE = 25
11+
num1 = randint(MIN_GEN_RANGE, MAX_GEN_RANGE)
12+
num2 = randint(MIN_GEN_RANGE, MAX_GEN_RANGE)
913
operation = choice('+-*')
10-
questions.append(f"{str(num1)} {operation} {str(num2)}")
14+
question = f"{str(num1)} {operation} {str(num2)}"
1115
match operation:
1216
case "+":
13-
right_answer.append(str(num1 + num2))
17+
answer = str(num1 + num2)
1418
case "-":
15-
right_answer.append(str(num1 - num2))
19+
answer = str(num1 - num2)
1620
case "*":
17-
right_answer.append(str(num1 * num2))
21+
answer = str(num1 * num2)
22+
return ((question, answer))

brain_games/games/even.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
from random import randint
22

3-
rules = 'Answer "yes" if the number is even, otherwise answer "no".'
4-
questions = []
5-
right_answer = []
6-
for _ in range(3):
7-
number = randint(1, 100)
8-
questions.append(number)
3+
4+
def displays_rules_game():
5+
print('Answer "yes" if the number is even, otherwise answer "no".')
6+
7+
8+
def generating_question_and_answer():
9+
MIN_GEN_RANGE = 1
10+
MAX_GEN_RANGE = 100
11+
number = randint(MIN_GEN_RANGE, MAX_GEN_RANGE)
12+
question = str(number)
913
if number % 2 == 0:
10-
right_answer.append('yes')
14+
answer = 'yes'
1115
else:
12-
right_answer.append('no')
16+
answer = 'no'
17+
return ((question, answer))

brain_games/games/gcd.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from random import randint
22

33

4-
def nod(num1, num2): # нахождение наибольшего общего делителя
4+
def finding_gcd(num1, num2): # нахождение наибольшего общего делителя
55
while num1 != 0 and num2 != 0:
66
if num1 > num2:
77
num1 = num1 % num2
@@ -10,11 +10,15 @@ def nod(num1, num2): # нахождение наибольшего общ
1010
return (num1 + num2)
1111

1212

13-
rules = "Find the greatest common divisor of given numbers."
14-
questions = []
15-
right_answer = []
16-
for _ in range(3):
17-
num1 = randint(1, 10)
18-
num2 = randint(1, 10)
19-
questions.append(f"{num1} {num2}")
20-
right_answer.append(str(nod(num1, num2)))
13+
def displays_rules_game():
14+
print("Find the greatest common divisor of given numbers.")
15+
16+
17+
def generating_question_and_answer():
18+
MIN_GEN_RANGE = 2
19+
MAX_GEN_RANGE = 10
20+
num1 = randint(MIN_GEN_RANGE, MAX_GEN_RANGE)
21+
num2 = randint(MIN_GEN_RANGE, MAX_GEN_RANGE)
22+
question = f"{num1} {num2}"
23+
answer = str(finding_gcd(num1, num2))
24+
return ((question, answer))

brain_games/games/prime.py

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33

44
# функция проверяет является ли число простым
5-
# если число простое -> False
6-
# если нет -> True
5+
# если число простое -> True
6+
# если нет -> False
77
def is_prime(number):
88
if number <= 1:
99
return False
@@ -13,13 +13,17 @@ def is_prime(number):
1313
return True
1414

1515

16-
rules = 'Answer "yes" if given number is prime. Otherwise answer "no".'
17-
questions = []
18-
right_answer = []
19-
for _ in range(3):
20-
number = randint(1, 100)
21-
questions.append(number)
16+
def displays_rules_game():
17+
print('Answer "yes" if given number is prime. Otherwise answer "no".')
18+
19+
20+
def generating_question_and_answer():
21+
MIN_GEN_RANGE = 1
22+
MAX_GEN_RANGE = 130
23+
number = randint(MIN_GEN_RANGE, MAX_GEN_RANGE)
24+
question = str(number)
2225
if is_prime(number):
23-
right_answer.append('yes')
26+
answer = 'yes'
2427
else:
25-
right_answer.append('no')
28+
answer = 'no'
29+
return ((question, answer))

brain_games/games/progression.py

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,37 @@
11
from random import randint
22

33

4-
def progress(first, step, empty): # создает арифметическую прогрессию
5-
temp = ''
4+
# функция создает арифметическую прогрессию
5+
def generating_a_progression(first: int, step: int, empty: int) -> str:
6+
question = ''
67
for i in range(1, 11):
78
if i == empty:
8-
temp += '.. '
9+
question += '.. '
910
else:
10-
temp += str(first + step * (i - 1)) + ' '
11-
temp = temp.strip()
12-
return (temp)
11+
question += str(first + step * (i - 1)) + ' '
12+
question = question.strip()
13+
return (question)
1314

1415

15-
rules = "What number is missing in the progression?"
16-
questions = []
17-
right_answer = []
18-
for _ in range(3):
19-
first = randint(2, 50)
20-
step = randint(2, 20)
21-
empty = randint(1, 10)
22-
right_answer.append(str(first + step * (empty - 1)))
23-
questions.append(progress(first, step, empty))
16+
def displays_rules_game():
17+
print("What number is missing in the progression?")
18+
19+
20+
def generating_question_and_answer():
21+
LEN_PROGRESSION = 10
22+
MIN_GEN_RANGE = 1
23+
MAX_GEN_RANGE = 50
24+
#
25+
first_element = randint(MIN_GEN_RANGE, MAX_GEN_RANGE)
26+
step = randint(MIN_GEN_RANGE, MAX_GEN_RANGE)
27+
index_of_missing_element = randint(MIN_GEN_RANGE, LEN_PROGRESSION)
28+
#
29+
answer = str(first_element + step * (index_of_missing_element - 1))
30+
question = ''
31+
for i in range(LEN_PROGRESSION):
32+
if i + 1 == index_of_missing_element:
33+
question += '.. '
34+
else:
35+
question += str(first_element + step * i) + ' '
36+
question = question.strip()
37+
return ((question, answer))

brain_games/scripts/brain_calc.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1-
import brain_games.engine_game
2-
from brain_games.games.calc import questions, right_answer, rules
1+
from brain_games.engine_game import run_game
2+
from brain_games.games.calc import (
3+
displays_rules_game,
4+
generating_question_and_answer,
5+
)
36

47

58
def main():
6-
brain_games.engine_game.engine(rules, questions, right_answer)
9+
run_game(displays_rules_game, generating_question_and_answer)
710

811

912
if __name__ == '__main__':

brain_games/scripts/brain_even.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1-
import brain_games.engine_game
2-
from brain_games.games.even import questions, right_answer, rules
1+
from brain_games.engine_game import run_game
2+
from brain_games.games.even import (
3+
displays_rules_game,
4+
generating_question_and_answer,
5+
)
36

47

58
def main():
6-
brain_games.engine_game.engine(rules, questions, right_answer)
9+
run_game(displays_rules_game, generating_question_and_answer)
710

811

912
if __name__ == '__main__':

brain_games/scripts/brain_games.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
import brain_games.cli
2-
3-
4-
def greeting():
5-
print('Welcome to the Brain Games!')
6-
2+
73

84
def main():
95
brain_games.cli.welcome_user()

0 commit comments

Comments
 (0)