From c6bd638dc3522d4358c09f6acdedd8451663d5cd Mon Sep 17 00:00:00 2001 From: hikarikumo Date: Sun, 17 Dec 2023 18:39:14 +0200 Subject: [PATCH] fixes Signed-off-by: hikarikumo --- brain_games/prime_game.py | 66 ++++++++++---------------------- brain_games/progression_game.py | 67 ++++++++++++--------------------- 2 files changed, 43 insertions(+), 90 deletions(-) diff --git a/brain_games/prime_game.py b/brain_games/prime_game.py index b60f7f0..7cf484e 100644 --- a/brain_games/prime_game.py +++ b/brain_games/prime_game.py @@ -1,71 +1,43 @@ -# -*- coding:utf-8 -*- -"""Function to check whether provided number is prime.""" - -import prompt import random +import prompt from cli import greet_first, welcome_user def question_prime(): - """ - Ask the question and return bool and value. - - Returns: - Question and number to guess to the user. - """ + """Ask the question and return a tuple (number_to_guess, user_answer).""" number_to_guess = random.randint(1, 100) - user_answer = prompt.string('Number: ' + str(number_to_guess) + '\n') + user_answer = prompt.string(f'Number: {number_to_guess}\n') return number_to_guess, user_answer def prime_goal(): - """Information about the goal of the prime game to the user.""" + """Provide information about the goal of the prime game to the user.""" print('Answer "yes" if given number is prime. Otherwise answer "no".\n') -def prime_div_result(number): - """ - Check if the number is prime. - - Input: - Accept one number as parameter. - - Returns: - Returns True if number is prime - False otherwise. - """ - check_sequence = [] - counter = 1 - while counter <= number: - if number % counter == 0: - check_sequence.append(counter) - counter += 1 - if len(check_sequence) > 2: - return False - return True +def is_prime(number): + """Check if the number is prime.""" + divisors = [count for count in range(1, number + 1) if number % count == 0] + return len(divisors) == 2 def prime_game(name): """Brain game prime logic.""" - counter = 1 - while counter <= 3: + for _ in range(3): random_number, user_answer = question_prime() - is_prime = prime_div_result(random_number) - if is_prime is True and user_answer == 'yes': - print('Your answer: ' + 'yes ' + str(random_number) + ' is prime') - print('Correct!') - if counter == 3: - print('Congratulations, ', name, '!', sep='') - return True - elif is_prime is False and user_answer == 'no': - print('Your answer: ' + 'yes ' + str(random_number) + ' is not prime') + prime_status = is_prime(random_number) + + expected_answer = 'yes' if prime_status else 'no' + + if user_answer == expected_answer: + print(f'Your answer: {user_answer} {random_number} is prime') print('Correct!') - if counter == 3: - print('Congratulations, ', name, '!', sep='') + if _ == 2: + print(f'Congratulations, {name}!') return True else: - print("Let's try again, ", name, '!', sep='') + print(f"Let's try again, {name}!") return False - counter += 1 def main(): @@ -77,4 +49,4 @@ def main(): if __name__ == '__main__': - main() \ No newline at end of file + main() diff --git a/brain_games/progression_game.py b/brain_games/progression_game.py index 5117167..5164196 100644 --- a/brain_games/progression_game.py +++ b/brain_games/progression_game.py @@ -1,6 +1,3 @@ -# -*- coding:utf-8 -*- -"""Function to calculate ariphmetic progression.""" - import random import prompt from cli import greet_first, welcome_user @@ -13,62 +10,52 @@ def progression_goal(): def string_progression(progression): """Convert list into parsed string.""" - counter = 0 - string = '' - while counter < len(progression): - if counter == len(progression) - 1: - string += str(progression[counter]) - return string - string += str(progression[counter]) + ', ' - counter += 1 + return ', '.join(map(str, progression)) def question_progression(): """ - Ask the question and return bool and value. + Ask the question and return a tuple (is_correct, missing_element). Returns: - True and value if answer is missing element. - False and value if answer is not missing element. + Tuple with True and the missing element if the answer is correct, + Tuple with False and the missing element otherwise. """ miss_element, progression = progression_show() - question_to_user = prompt.string('Question: ' + string_progression(progression) + '\n') - if question_to_user == str(miss_element): - return True, miss_element - return False, miss_element + question_to_user = prompt.string(f'Question: {string_progression(progression)}\n') + return question_to_user == str(miss_element), miss_element def progression_show(): """ - Create ariphmetic progression. + Create arithmetic progression. Returns: Progression with *hidden* element + value of that hidden element. """ - random_number1 = random.randint(1, 10) - random_number2 = random.randint(1, 10) - random_number3 = random.randint(1, 10) - progression = progression_calc(random_number2, random_number3) - keep_value = progression[random_number1] - progression[random_number1] = '..' + random_index = random.randint(1, 10) + random_step = random.randint(1, 10) + random_initial = random.randint(1, 10) + + progression = progression_calc(random_step, random_initial) + keep_value = progression[random_index] + progression[random_index] = '..' + return keep_value, progression def progression_game(name): """Perform progression game logic.""" - counter = 1 - while counter <= 3: + for _ in range(3): calc_value, miss_element = question_progression() - if calc_value is True: - print('Your answer: ' + str(miss_element)) - print('Correct!') - if counter == 3: - print('Congratulations, ', name, '!', sep='') + if calc_value: + print(f'Your answer: {miss_element}\nCorrect!') + if _ == 2: + print(f'Congratulations, {name}!') return True else: - print("Let's try again, ", name, '!', sep='') + print(f"Let's try again, {name}!") return False - counter += 1 def progression_calc(random_step, random_initial): @@ -81,15 +68,9 @@ def progression_calc(random_step, random_initial): Returns: Returns progression list. """ - progression = [] - progression.append(random_initial) progression_length = 11 - counter = 1 progression_member = random_initial - while counter < progression_length: - progression_member += random_step - progression.append(progression_member) - counter += 1 + progression = [progression_member + i * random_step for i in range(progression_length)] return progression @@ -98,7 +79,7 @@ def main(): Run brain-progression game. Returns: - Return and verify the proper elemen inside of the ariphmetic progressio. + Return and verify the proper element inside of the arithmetic progression. """ greet_first() progression_goal() @@ -107,4 +88,4 @@ def main(): if __name__ == '__main__': - main() \ No newline at end of file + main()