From 5fa152edcbfc0b8242c1b9369ef998e5c0ec49ef Mon Sep 17 00:00:00 2001 From: Dmitry_Perexozhev Date: Fri, 1 Dec 2023 01:57:34 +0300 Subject: [PATCH] Correct the edits for project No. 1 --- README.md | 14 ++++++------- brain_games/engine.py | 12 +++++------ brain_games/games/game_calc.py | 26 +++++++++++++++++++----- brain_games/games/game_even.py | 11 +++++++--- brain_games/games/game_gcd.py | 18 ++++++++++------ brain_games/games/game_prime.py | 25 +++++++++++++++-------- brain_games/games/game_progression.py | 20 ++++++++++++------ brain_games/scripts/brain_calc.py | 5 ++--- brain_games/scripts/brain_even.py | 5 ++--- brain_games/scripts/brain_gcd.py | 5 ++--- brain_games/scripts/brain_prime.py | 5 ++--- brain_games/scripts/brain_progression.py | 5 ++--- 12 files changed, 94 insertions(+), 57 deletions(-) diff --git a/README.md b/README.md index 1fabae5..cdf376e 100644 --- a/README.md +++ b/README.md @@ -13,8 +13,8 @@ ### Installation Execute in a terminal in the root directory: -python3 -m pip install dist/hexlet_code-0.1.0-py3-none-any.whl -or command: make package-install +python3 -m pip install dist/hexlet_code-0.1.0-py3-none-any.whl\ +or command: make package-install\ asciinema installation (https://asciinema.org/a/Pt2fuw4TDiNtm143Nneowmt73) p.s. On my system there is a conflict between Ubuntu and pip so I activate Poetry, @@ -22,13 +22,13 @@ you may not need to do this ### How it works To start the game you need to execute one of the commands: -1. brain-even +1. brain-even\ asciinema game brain-even (https://asciinema.org/a/ai5jn7A3lrUcdwqXUyDEqt4O9) -2. brain-calc +2. brain-calc\ asciinema game brain-calc (https://asciinema.org/a/5zjMH4tBlli6rwRq09TIg6QSe) -3. brain-gcd +3. brain-gcd\ asciinema game brain-gcd (https://asciinema.org/a/JrAlVYJD3mrWSkFc0ZK8FjZ4k) -4. brain-progression +4. brain-progression\ asciinema game brain-progression (https://asciinema.org/a/eZD5xzdn7lVDwY798sVaUWOyg) -5. brain-prime +5. brain-prime\ asciinema game brain-prime (https://asciinema.org/a/adu91gryjLGUSof43guMI5htV) diff --git a/brain_games/engine.py b/brain_games/engine.py index e4a2f2b..87d2a93 100644 --- a/brain_games/engine.py +++ b/brain_games/engine.py @@ -1,25 +1,23 @@ import prompt -def welcome_user(): +def engine(brain_game): print('Welcome to the Brain Games!') - global name name = prompt.string('May I have your name? ') print(f'Hello, {name}!') - - -def engine(brain_game): + print(brain_game.DESCRIPTION) count_correct_answers = 0 while count_correct_answers < 3: - correct_answer = brain_game() + correct_answer = brain_game.generate_round() user_answer = prompt.string('Your answer: ') if user_answer == correct_answer: print('Correct!') count_correct_answers += 1 + count_correct_answers == 3 else: print(f"'{user_answer}' is wrong answer ;(." f" Correct answer was '{correct_answer}'.") print(f"Let's try again, {name}!") break - if count_correct_answers == 3: + else: print(f"Congratulations, {name}!") diff --git a/brain_games/games/game_calc.py b/brain_games/games/game_calc.py index 4079ee6..baa2a30 100644 --- a/brain_games/games/game_calc.py +++ b/brain_games/games/game_calc.py @@ -1,17 +1,33 @@ from random import randint, choice +DESCRIPTION = 'What is the result of the expression?' -def game_calc(): - print('What is the result of the expression?') + +def add(x, y): + return x + y + + +def multiply(x, y): + return x * y + + +def subtraction(x, y): + return x - y + + +def generate_round(): first_random_number = randint(1, 20) second_random_number = randint(1, 20) random_operation = choice('+-*') if random_operation == '+': - correct_answer = str(first_random_number + second_random_number) + correct_answer =\ + str(add(first_random_number, second_random_number)) elif random_operation == '-': - correct_answer = str(first_random_number - second_random_number) + correct_answer =\ + str(subtraction(first_random_number, second_random_number)) elif random_operation == '*': - correct_answer = str(first_random_number * second_random_number) + correct_answer =\ + str(multiply(first_random_number, second_random_number)) print(f'Question: {first_random_number} {random_operation} ' f'{second_random_number}') return correct_answer diff --git a/brain_games/games/game_even.py b/brain_games/games/game_even.py index 8d9e530..4b19d2d 100644 --- a/brain_games/games/game_even.py +++ b/brain_games/games/game_even.py @@ -1,9 +1,14 @@ from random import randint +DESCRIPTION = 'Answer "yes" if the number is even, otherwise answer "no".' -def game_even(): - print('Answer "yes" if the number is even, otherwise answer "no".') + +def is_even(number): + return number % 2 == 0 + + +def generate_round(): random_number = randint(1, 100) - correct_answer = ('yes', 'no')[random_number % 2] + correct_answer = 'yes' if is_even(random_number) else 'no' print(f'Question: {random_number}') return correct_answer diff --git a/brain_games/games/game_gcd.py b/brain_games/games/game_gcd.py index 851f14d..e0dd31e 100644 --- a/brain_games/games/game_gcd.py +++ b/brain_games/games/game_gcd.py @@ -1,13 +1,19 @@ from random import randint +DESCRIPTION = 'Find the greatest common divisor of given numbers.' -def game_gcd(): - print('Find the greatest common divisor of given numbers.') + +def find_divisors(number): + divisors = [i for i in range(1, number + 1) if number % i == 0] + return divisors + + +def generate_round(): first_random_number = randint(1, 100) second_random_number = randint(1, 100) - correct_answer = 1 - for i in range(2, min(first_random_number, second_random_number) + 1): - if first_random_number % i == 0 and second_random_number % i == 0: - correct_answer = i + divisors_of_first_random_number = set(find_divisors(first_random_number)) + divisors_of_second_random_number = set(find_divisors(second_random_number)) + correct_answer =\ + max(divisors_of_first_random_number & divisors_of_second_random_number) print(f'Question: {first_random_number} {second_random_number}') return str(correct_answer) diff --git a/brain_games/games/game_prime.py b/brain_games/games/game_prime.py index a707611..d5fc063 100644 --- a/brain_games/games/game_prime.py +++ b/brain_games/games/game_prime.py @@ -1,13 +1,22 @@ from random import randint +DESCRIPTION = 'Answer "yes" if given number is prime. Otherwise answer "no".' -def game_prime(): - print('Answer "yes" if given number is prime. Otherwise answer "no".') - random_number = randint(3, 100) - correct_answer = 'yes' - for i in range(2, random_number): - if random_number % i == 0: - correct_answer = 'no' - break + +def is_prime(number): + if number >= 2: + if number == 2: + return True + divider = 2 + while number % divider != 0: + divider += 1 + return number == divider + else: + return False + + +def generate_round(): + random_number = randint(2, 100) + correct_answer = 'yes' if is_prime(random_number) else 'no' print(f'Question: {random_number}') return correct_answer diff --git a/brain_games/games/game_progression.py b/brain_games/games/game_progression.py index 85edfe2..d8805be 100644 --- a/brain_games/games/game_progression.py +++ b/brain_games/games/game_progression.py @@ -1,14 +1,22 @@ from random import randint +DESCRIPTION = 'What number is missing in the progression?' -def game_progression(): - print('What number is missing in the progression?') + +def get_sequence(start, step, number_of_steps): + sequence = [] + for i in range(number_of_steps): + sequence.append(start + step * i) + return sequence + + +def generate_round(): random_start_number = randint(1, 20) random_step = randint(1, 10) - random_sequence = [] - for i in range(10): - random_sequence.append(random_start_number + random_step * i) - random_index_of_seq = randint(0, 9) + number_of_steps = 10 + random_sequence =\ + get_sequence(random_start_number, random_step, number_of_steps) + random_index_of_seq = randint(0, number_of_steps - 1) correct_answer = str(random_sequence[random_index_of_seq]) random_sequence[random_index_of_seq] = '..' print('Question:', *random_sequence) diff --git a/brain_games/scripts/brain_calc.py b/brain_games/scripts/brain_calc.py index 5d4eccc..8cb5acd 100644 --- a/brain_games/scripts/brain_calc.py +++ b/brain_games/scripts/brain_calc.py @@ -1,10 +1,9 @@ #!/usr/bin/env python3 -from brain_games.games.game_calc import game_calc -from brain_games.engine import welcome_user, engine +from brain_games.games import game_calc +from brain_games.engine import engine def main(): - welcome_user() engine(game_calc) diff --git a/brain_games/scripts/brain_even.py b/brain_games/scripts/brain_even.py index 5cb2cf2..44c2ba6 100644 --- a/brain_games/scripts/brain_even.py +++ b/brain_games/scripts/brain_even.py @@ -1,10 +1,9 @@ #!/usr/bin/env python3 -from brain_games.games.game_even import game_even -from brain_games.engine import welcome_user, engine +from brain_games.games import game_even +from brain_games.engine import engine def main(): - welcome_user() engine(game_even) diff --git a/brain_games/scripts/brain_gcd.py b/brain_games/scripts/brain_gcd.py index 53cc60e..aa40586 100644 --- a/brain_games/scripts/brain_gcd.py +++ b/brain_games/scripts/brain_gcd.py @@ -1,10 +1,9 @@ #!/usr/bin/env python3 -from brain_games.games.game_gcd import game_gcd -from brain_games.engine import welcome_user, engine +from brain_games.games import game_gcd +from brain_games.engine import engine def main(): - welcome_user() engine(game_gcd) diff --git a/brain_games/scripts/brain_prime.py b/brain_games/scripts/brain_prime.py index 801baec..7d70fe7 100644 --- a/brain_games/scripts/brain_prime.py +++ b/brain_games/scripts/brain_prime.py @@ -1,10 +1,9 @@ #!/usr/bin/env python3 -from brain_games.games.game_prime import game_prime -from brain_games.engine import welcome_user, engine +from brain_games.games import game_prime +from brain_games.engine import engine def main(): - welcome_user() engine(game_prime) diff --git a/brain_games/scripts/brain_progression.py b/brain_games/scripts/brain_progression.py index 54ccfa1..8dd18b3 100644 --- a/brain_games/scripts/brain_progression.py +++ b/brain_games/scripts/brain_progression.py @@ -1,10 +1,9 @@ #!/usr/bin/env python3 -from brain_games.games.game_progression import game_progression -from brain_games.engine import welcome_user, engine +from brain_games.games import game_progression +from brain_games.engine import engine def main(): - welcome_user() engine(game_progression)