diff --git a/brain_games/calc_game.py b/brain_games/calc_game.py index ea603ad..49628f5 100644 --- a/brain_games/calc_game.py +++ b/brain_games/calc_game.py @@ -5,102 +5,62 @@ import prompt from cli import greet_first, welcome_user - -def calc_greet_first(): - """First greeting message.""" - print('What is the result of the expression?\n') - - -def calc_game_summ(): - """Summary operation.""" - number1 = random.randint(0, 100) - number2 = random.randint(0, 100) - summ_result = number1 + number2 - return summ_result, number1, number2 +OPERATIONS = { + 'summation': ('+', lambda x, y: x + y), + 'subtraction': ('-', lambda x, y: x - y), + 'multiplication': ('*', lambda x, y: x * y), +} -def summ_check(): - """Check calculation of summary by user.""" - answer_sum, number1, number2 = calc_game_summ() - question_sum = prompt.string('Question: ' + str(number1) + ' + ' + str(number2) + '\n') - if question_sum == str(answer_sum): - print('Your answer: ' + question_sum) - print('Correct!') - return True - print("'" + str(question_sum) + "' " + "is wrong answer ;(. Correct answer was " + "'" + str(answer_sum) + "'") - return False +def get_random_numbers(): + return random.randint(0, 100), random.randint(0, 100) -def calc_game_subtract(): - """Subtract operation.""" - number3 = random.randint(0, 100) - number4 = random.randint(0, 100) - answer_subtr = number3 - number4 - return answer_subtr, number3, number4 +def generate_question(operation): + symbol, _ = OPERATIONS[operation] + num1, num2 = get_random_numbers() + return f'Question: {num1} {symbol} {num2}\n', num1, num2 -def subtract_check(): - """Check calculation of substract by user.""" - answer_subtr, number3, number4 = calc_game_subtract() - question_subtr = prompt.string('Question: ' + str(number3) + ' - ' + str(number4) + '\n') - if question_subtr == str(answer_subtr): - print('Your answer: ' + question_subtr) +def check_answer(question, answer, correct_answer): + if answer == str(correct_answer): + print(f'Your answer: {answer}') print('Correct!') return True - print("'" + str(question_subtr) + "' " + "is wrong answer ;(. Correct answer was " + "'" + str(answer_subtr) + "'") + print(f"'{answer}' is wrong answer ;(. Correct answer was '{correct_answer}'") return False -def calc_game_multiplication(): - """Multiplication operation.""" - number5 = random.randint(0, 10) - number6 = random.randint(0, 10) - answer_mult = number5 * number6 - return answer_mult, number5, number6 - - -def multiplication_check(): - """Check calculation of multiplication by user.""" - answer_mult, number5, number6 = calc_game_multiplication() - question_mult = prompt.string('Question: ' + str(number5) + ' * ' + str(number6) + '\n') - if question_mult == str(answer_mult): - print('Your answer: ' + question_mult) - print('Correct!') - return True - print("'" + str(question_mult) + "' " + "is wrong answer ;(. Correct answer was " + "'" + str(answer_mult) + "'") - return False +def play_game(operation): + question, num1, num2 = generate_question(operation) + correct_answer = OPERATIONS[operation][1](num1, num2) + user_answer = prompt.string(question) + return check_answer(question, user_answer, correct_answer) def user_fail_message(name): - """Report fail to user.""" - print("Let's try again, ", name, '!', sep='') + print(f"Let's try again, {name}!") def calc_game_logic(name): - """Calc game logic function.""" counter = 1 while counter <= 3: - if summ_check() is False: - user_fail_message(name) - break - elif subtract_check() is False: - user_fail_message(name) - break - elif multiplication_check() is False: - user_fail_message(name) - break - elif counter == 3: - print('Congratulations, ', name, '!', sep='') + operations = list(OPERATIONS.keys()) + for operation in operations: + if not play_game(operation): + user_fail_message(name) + return + if counter == 3: + print(f'Congratulations, {name}!') counter += 1 def main(): - """Run brain-calc game.""" greet_first() - calc_greet_first() + print('What is the result of the expression?\n') name = welcome_user() calc_game_logic(name) if __name__ == '__main__': - main() \ No newline at end of file + main()