Skip to content

Commit 2d0c722

Browse files
committed
Проект выполнен, переработано управление работы игр. Теперь оно производится из одного файла - src.py
1 parent 862102f commit 2d0c722

File tree

16 files changed

+179
-181
lines changed

16 files changed

+179
-181
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
### CodeClimate status:
66
[![Maintainability](https://api.codeclimate.com/v1/badges/83457b9fd7b67208524c/maintainability)](https://codeclimate.com/github/BiscayN/python-project-49/maintainability)
77

8-
### Brain-Even Test:
9-
[![asciicast](https://asciinema.org/a/qZlswideTs2fVDc6qpCy2EsIc.svg)](https://asciinema.org/a/qZlswideTs2fVDc6qpCy2EsIc)
8+
### Installation + Brain-Even Test:
9+
[![asciicast](https://asciinema.org/a/o8piEMzeBVbrdPDk2HWSqF5Zz.svg)](https://asciinema.org/a/o8piEMzeBVbrdPDk2HWSqF5Zz)
1010

1111
### Brain-Calc Test:
1212
[![asciicast](https://asciinema.org/a/mkMowswaIrjQpgW8QyvtBGrHi.svg)](https://asciinema.org/a/mkMowswaIrjQpgW8QyvtBGrHi)

brain_games/cli.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33

44
def welcome_user():
55
name = string('May I have your name? ')
6-
return (f'Hello, {name}!')
6+
print(f'Hello, {name}!')

brain_games/games/__init__.py

Whitespace-only changes.

brain_games/games/calc.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from random import randint
2+
from random import choice
3+
4+
QUESTION = 'What is the result of the expression?'
5+
6+
7+
def get_values():
8+
num1, num2 = randint(1, 50), randint(1, 50)
9+
operator = choice(['+', '-', '*'])
10+
return num1, num2, operator
11+
12+
13+
def conclusion():
14+
num1, num2, operator = get_values()
15+
num_question = f'{max(num1, num2)} {operator} {min(num1, num2)}'
16+
correct_answer = str(eval(num_question))
17+
return correct_answer, num_question

brain_games/games/even.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
from random import randint
2+
3+
QUESTION = 'Answer "yes" if the number is even, otherwise answer "no".'
4+
5+
6+
def is_even(number):
7+
if number % 2 == 0:
8+
return True
9+
else:
10+
return False
11+
12+
13+
def get_number():
14+
return randint(1, 100)
15+
16+
17+
def conclusion():
18+
num_question = get_number()
19+
if is_even(num_question):
20+
correct_answer = 'yes'
21+
else:
22+
correct_answer = 'no'
23+
return correct_answer, num_question

brain_games/games/gcd.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from random import randint
2+
from math import gcd
3+
4+
QUESTION = 'Find the greatest common divisor of given numbers.'
5+
6+
7+
def get_numbers():
8+
num1, num2 = randint(1, 50), randint(1, 50)
9+
return num1, num2
10+
11+
12+
def conclusion():
13+
num1, num2 = get_numbers()
14+
num_question = f'{num1} {num2}'
15+
correct_answer = str(gcd(num1, num2))
16+
return correct_answer, num_question

brain_games/games/prime.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
from random import randint
2+
3+
QUESTION = 'Answer "yes" if given number is prime. Otherwise answer "no".'
4+
5+
6+
def is_prime(number):
7+
if number == 2:
8+
return True
9+
for i in range(3, int(number ** 0.5) + 1):
10+
if number % i == 0:
11+
return False
12+
return True
13+
14+
15+
def get_values():
16+
return randint(2, 100)
17+
18+
19+
def conclusion():
20+
num_question = get_values()
21+
if is_prime(num_question):
22+
correct_answer = 'yes'
23+
else:
24+
correct_answer = 'no'
25+
return correct_answer, num_question

brain_games/games/progression.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from random import randint
2+
3+
QUESTION = 'What number is missing in the progression?'
4+
5+
6+
def get_progression():
7+
progression = []
8+
start = randint(1, 20)
9+
step = randint(1, 5)
10+
for i in range(10):
11+
progression.append(start)
12+
start += step
13+
prog_str = ' '.join([str(num) for num in progression])
14+
return prog_str, progression
15+
16+
17+
def conclusion():
18+
prog_str, progression = get_progression()
19+
correct_answer = str(progression[randint(0, 9)])
20+
num_question = prog_str.replace(correct_answer, '..', 1)
21+
return correct_answer, num_question

brain_games/scripts/brain_calc.py

Lines changed: 7 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,11 @@
1-
from random import randint
2-
from random import choice
3-
from sys import exit
4-
from prompt import string
1+
#!/usr/bin/env python3
2+
from brain_games.games import calc
3+
from brain_games.src import solution
54

6-
START_QUESTION = 'What is the result of the expression?'
75

8-
9-
def get_values():
10-
num1, num2 = randint(1, 50), randint(1, 50)
11-
operator = choice(['+', '-', '*'])
12-
return num1, num2, operator
6+
def main():
7+
solution(calc)
138

149

15-
def main():
16-
print('Welcome to the Brain Games!')
17-
name = string('May I have your name? ')
18-
print(f'Hello, {name}!')
19-
print(START_QUESTION)
20-
counter = 0
21-
while counter < 3:
22-
num1, num2, operator = get_values()
23-
question = f'{max(num1, num2)} {operator} {min(num1, num2)}'
24-
print(f'Question: {max(num1, num2)} {operator} {min(num1, num2)}')
25-
user_answer = string('Your answer: ')
26-
if str(eval(question)) == str(user_answer):
27-
print('Correct!')
28-
else:
29-
print(f"'{user_answer}' is wrong answer "
30-
f";(. Correct answer was '{eval(question)}'")
31-
print(f"Let's try again, {name}!")
32-
exit(0)
33-
counter += 1
34-
print(f'Congratulations, {name}!')
35-
exit(0)
10+
if __name__ == '__main__':
11+
main()

brain_games/scripts/brain_even.py

Lines changed: 7 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,11 @@
1-
from random import randint
2-
from sys import exit
3-
from prompt import string
1+
#!/usr/bin/env python3
2+
from brain_games.games import even
3+
from brain_games.src import solution
44

5-
QUESTION = 'Answer "yes" if the number is even, otherwise answer "no".'
65

7-
8-
def get_number():
9-
return randint(1, 100)
6+
def main():
7+
solution(even)
108

119

12-
def main():
13-
print('Welcome to the Brain Games!')
14-
name = string('May I have your name? ')
15-
print(f'Hello, {name}!')
16-
print(QUESTION)
17-
counter = 0
18-
while counter < 3:
19-
num = get_number()
20-
print(f'Question: {num}')
21-
user_answer = string('Your answer: ')
22-
if not num % 2:
23-
if user_answer == 'yes':
24-
print('Correct!')
25-
else:
26-
print(f"'{user_answer}' is wrong answer "
27-
f";(. Correct answer was 'yes'.")
28-
print(f"Let's try again, {name}!")
29-
exit(0)
30-
else:
31-
if user_answer == 'no':
32-
print('Correct!')
33-
else:
34-
print(f"'{user_answer}' is wrong answer "
35-
f";(. Correct answer was 'no'.")
36-
print(f"Let's try again, {name}!")
37-
exit(0)
38-
counter += 1
39-
print(f'Congratulations, {name}!')
40-
exit(0)
10+
if __name__ == '__main__':
11+
main()

0 commit comments

Comments
 (0)