Skip to content

Commit

Permalink
Сhange the project structure
Browse files Browse the repository at this point in the history
Add a games module. Add engine.py
  • Loading branch information
Dmitry-Perexozhev committed Nov 26, 2023
1 parent 97c443e commit 2d046d5
Show file tree
Hide file tree
Showing 24 changed files with 112 additions and 231 deletions.
Binary file added brain_games/__pycache__/engine.cpython-311.pyc
Binary file not shown.
25 changes: 25 additions & 0 deletions brain_games/engine.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import prompt


def welcome_user():
print('Welcome to the Brain Games!')
global name
name = prompt.string('May I have your name? ')
print(f'Hello, {name}!')


def engine(brain_game):
count_correct_answers = 0
while count_correct_answers < 3:
correct_answer = brain_game()
user_answer = prompt.string('Your answer: ')
if user_answer == correct_answer:
print('Correct!')
count_correct_answers += 1
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:
print(f"Congratulations, {name}!")
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
17 changes: 17 additions & 0 deletions brain_games/games/game_calc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from random import randint, choice


def game_calc():
print('What is the result of the expression?')
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)
elif random_operation == '-':
correct_answer = str(first_random_number - second_random_number)
elif random_operation == '*':
correct_answer = str(first_random_number * second_random_number)
print(f'Question: {first_random_number} {random_operation} {second_random_number}')
return correct_answer

10 changes: 10 additions & 0 deletions brain_games/games/game_even.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from random import randint


def game_even():
print('Answer "yes" if the number is even, otherwise answer "no".')
random_number = randint(1, 100)
correct_answer = ('yes', 'no')[random_number % 2]
print(f'Question: {random_number}')
return correct_answer

14 changes: 14 additions & 0 deletions brain_games/games/game_gcd.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from random import randint


def game_gcd():
print('Find the greatest common divisor of given numbers.')
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
print(f'Question: {first_random_number} {second_random_number}')
return str(correct_answer)

14 changes: 14 additions & 0 deletions brain_games/games/game_prime.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from random import randint


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
print(f'Question: {random_number}')
return correct_answer

16 changes: 16 additions & 0 deletions brain_games/games/game_progression.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from random import randint


def game_progression():
print('What number is missing in the progression?')
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)
correct_answer = str(random_sequence[random_index_of_seq])
random_sequence[random_index_of_seq] = '..'
print('Question:', *random_sequence)
return correct_answer

Binary file modified brain_games/scripts/__pycache__/brain_calc.cpython-311.pyc
Binary file not shown.
Binary file modified brain_games/scripts/__pycache__/brain_even.cpython-311.pyc
Binary file not shown.
Binary file not shown.
Binary file modified brain_games/scripts/__pycache__/brain_prime.cpython-311.pyc
Binary file not shown.
Binary file not shown.
52 changes: 3 additions & 49 deletions brain_games/scripts/brain_calc.py
Original file line number Diff line number Diff line change
@@ -1,57 +1,11 @@
#!/usr/bin/env python3
import prompt
from random import randint, choice


def welcome_user():
global name
name = prompt.string('May I have your name? ')
print(f'Hello, {name}!')


def greeting():
print('Welcome to the Brain Games!')


def try_again(name):
print(f"Let's try again, {name}!")


def congratulations(name):
print(f"Congratulations, {name}!")


def brain_calc():
print('What is the result of the expression?')
count_correct_answers = 0
while count_correct_answers < 3:
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)
elif random_operation == '-':
correct_answer = str(first_random_number - second_random_number)
elif random_operation == '*':
correct_answer = str(first_random_number * second_random_number)
print(f'Question: {first_random_number} {random_operation} {second_random_number}')
user_answer = prompt.string('Your answer: ')
if user_answer == correct_answer:
print('Correct!')
count_correct_answers += 1
else:
print(f"'{user_answer}' is wrong answer ;(."
f" Correct answer was '{correct_answer}'.")
try_again(name)
break
if count_correct_answers == 3:
congratulations(name)
from brain_games.games.game_calc import game_calc
from brain_games.engine import welcome_user, engine


def main():
greeting()
welcome_user()
brain_calc()
engine(game_calc)


if __name__ == '__main__':
Expand Down
45 changes: 3 additions & 42 deletions brain_games/scripts/brain_even.py
Original file line number Diff line number Diff line change
@@ -1,50 +1,11 @@
#!/usr/bin/env python3
import prompt
from random import randint


def welcome_user():
global name
name = prompt.string('May I have your name? ')
print(f'Hello, {name}!')


def greeting():
print('Welcome to the Brain Games!')


def try_again(name):
print(f"Let's try again, {name}!")


def congratulations(name):
print(f"Congratulations, {name}!")


def brain_even():
print('Answer "yes" if the number is even, otherwise answer "no".')
count_correct_answers = 0
while count_correct_answers < 3:
random_number = randint(1, 100)
correct_answer = ('yes', 'no')[random_number % 2]
print(f'Question: {random_number}')
user_answer = prompt.string('Your answer: ')
if user_answer == correct_answer:
print('Correct!')
count_correct_answers += 1
else:
print(f"'{user_answer}' is wrong answer ;(."
f" Correct answer was '{correct_answer}'.")
try_again(name)
break
if count_correct_answers == 3:
congratulations(name)
from brain_games.games.game_even import game_even
from brain_games.engine import welcome_user, engine


def main():
greeting()
welcome_user()
brain_even()
engine(game_even)


if __name__ == '__main__':
Expand Down
50 changes: 4 additions & 46 deletions brain_games/scripts/brain_gcd.py
Original file line number Diff line number Diff line change
@@ -1,53 +1,11 @@
import prompt
from random import randint, choice


def welcome_user():
global name
name = prompt.string('May I have your name? ')
print(f'Hello, {name}!')


def greeting():
print('Welcome to the Brain Games!')


def try_again(name):
print(f"Let's try again, {name}!")


def congratulations(name):
print(f"Congratulations, {name}!")


def brain_gcd():
print('Find the greatest common divisor of given numbers.')
count_correct_answers = 0
while count_correct_answers < 3:
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
print(f'Question: {first_random_number} {second_random_number}')
user_answer = prompt.string('Your answer: ')
if user_answer == str(correct_answer):
print('Correct!')
count_correct_answers += 1
else:
print(f"'{user_answer}' is wrong answer ;(."
f" Correct answer was '{correct_answer}'.")
try_again(name)
break
if count_correct_answers == 3:
congratulations(name)
#!/usr/bin/env python3
from brain_games.games.game_gcd import game_gcd
from brain_games.engine import welcome_user, engine


def main():
greeting()
welcome_user()
brain_gcd()
engine(game_gcd)


if __name__ == '__main__':
Expand Down
49 changes: 3 additions & 46 deletions brain_games/scripts/brain_prime.py
Original file line number Diff line number Diff line change
@@ -1,54 +1,11 @@
#!/usr/bin/env python3
import prompt
from random import randint, choice


def welcome_user():
global name
name = prompt.string('May I have your name? ')
print(f'Hello, {name}!')


def greeting():
print('Welcome to the Brain Games!')


def try_again(name):
print(f"Let's try again, {name}!")


def congratulations(name):
print(f"Congratulations, {name}!")


def brain_prime():
print('Answer "yes" if given number is prime. Otherwise answer "no".')
count_correct_answers = 0
while count_correct_answers < 3:
random_number = randint(3, 100)
correct_answer = 'yes'
for i in range(2, random_number):
if random_number % i == 0:
correct_answer = 'no'
break
print(f'Question: {random_number}')
user_answer = prompt.string('Your answer: ')
if user_answer == correct_answer:
print('Correct!')
count_correct_answers += 1
else:
print(f"'{user_answer}' is wrong answer ;(."
f" Correct answer was '{correct_answer}'.")
try_again(name)
break
if count_correct_answers == 3:
congratulations(name)
from brain_games.games.game_prime import game_prime
from brain_games.engine import welcome_user, engine


def main():
greeting()
welcome_user()
brain_prime()
engine(game_prime)


if __name__ == '__main__':
Expand Down
51 changes: 3 additions & 48 deletions brain_games/scripts/brain_progression.py
Original file line number Diff line number Diff line change
@@ -1,56 +1,11 @@
#!/usr/bin/env python3
import prompt
from random import randint, choice


def welcome_user():
global name
name = prompt.string('May I have your name? ')
print(f'Hello, {name}!')


def greeting():
print('Welcome to the Brain Games!')


def try_again(name):
print(f"Let's try again, {name}!")


def congratulations(name):
print(f"Congratulations, {name}!")


def brain_progression():
print('What number is missing in the progression?')
count_correct_answers = 0
while count_correct_answers < 3:
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)
correct_answer = str(random_sequence[random_index_of_seq])
random_sequence[random_index_of_seq] = '..'
print('Question:', *random_sequence)
user_answer = prompt.string('Your answer: ')
if user_answer == correct_answer:
print('Correct!')
count_correct_answers += 1
else:
print(f"'{user_answer}' is wrong answer ;(."
f" Correct answer was '{correct_answer}'.")
try_again(name)
break
if count_correct_answers == 3:
congratulations(name)
from brain_games.games.game_progression import game_progression
from brain_games.engine import welcome_user, engine


def main():
greeting()
welcome_user()
brain_progression()
engine(game_progression)


if __name__ == '__main__':
Expand Down
Binary file modified dist/hexlet_code-0.1.0-py3-none-any.whl
Binary file not shown.
Binary file modified dist/hexlet_code-0.1.0.tar.gz
Binary file not shown.

0 comments on commit 2d046d5

Please sign in to comment.