Skip to content

Commit bee9bdb

Browse files
author
shmelevik
committed
add calc game, add "games" dir
1 parent eda7f23 commit bee9bdb

File tree

12 files changed

+149
-17
lines changed

12 files changed

+149
-17
lines changed

README.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,14 @@
22
[![Actions Status](https://github.com/Shmelevick/python-project-49/actions/workflows/hexlet-check.yml/badge.svg)](https://github.com/Shmelevick/python-project-49/actions)[![Maintainability](https://api.codeclimate.com/v1/badges/48b30ee9e092d6ff1e60/maintainability)](https://codeclimate.com/github/Shmelevick/python-project-49/maintainability)
33
[![Test Coverage](https://api.codeclimate.com/v1/badges/48b30ee9e092d6ff1e60/test_coverage)](https://codeclimate.com/github/Shmelevick/python-project-49/test_coverage)
44

5-
## 📽️ Example in Action
5+
## 📽️ Sample of "Is even" game:
66

77
Check out the demo of the package in action by clicking on the [asciinema recording](https://asciinema.org/a/sOCkYp6t3giKi2DkTCcgzuYUf) below:
88

9-
![asciinema demo](https://asciinema.org/a/sOCkYp6t3giKi2DkTCcgzuYUf.svg)
9+
![asciinema demo](https://asciinema.org/a/sOCkYp6t3giKi2DkTCcgzuYUf)
10+
11+
## 📽️ Sample of "Calculator" game:
12+
13+
Check out the demo of the package in action by clicking on the [asciinema recording](https://asciinema.org/a/pGluyplksGL0l4Qu8sPeBipvZ) below:
14+
15+
![asciinema demo](https://asciinema.org/a/pGluyplksGL0l4Qu8sPeBipvZ)
517 Bytes
Binary file not shown.
1.76 KB
Binary file not shown.
1.48 KB
Binary file not shown.
666 Bytes
Binary file not shown.
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import operator
2+
from random import choice, randint
3+
from time import sleep
4+
5+
from prompt import string
6+
7+
from brain_games.cli import welcome_user
8+
from brain_games.wrong_answer_output import wrong_answer_output
9+
10+
11+
def calc_game(name):
12+
13+
ops = {
14+
"+": operator.add,
15+
"-": operator.sub,
16+
"*": operator.mul,
17+
}
18+
19+
sleep(1)
20+
print('What is the result of the expression?')
21+
sleep(1)
22+
23+
correct_guesses = 0
24+
25+
while correct_guesses < 3:
26+
num_1, num_2 = randint(0, 100), randint(1, 20)
27+
oper = choice(('+', '-', '*'))
28+
29+
answer = string(f'Question: {num_1} {oper} {num_2}\nYour answer: ')
30+
31+
correct_answer = str(ops[oper](num_1, num_2))
32+
33+
if answer != correct_answer:
34+
wrong_answer_output(answer, correct_answer, name)
35+
36+
sleep(2)
37+
38+
continue
39+
40+
print('Correct!\n')
41+
sleep(1)
42+
43+
correct_guesses += 1
44+
45+
print(f'Congratulations, {name}!\n')
46+
47+
48+
def main():
49+
name = welcome_user()
50+
calc_game(name)
51+
52+
53+
if __name__ == "__main__":
54+
main()
Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from prompt import string
55

66
from brain_games.cli import welcome_user
7+
from brain_games.wrong_answer_output import wrong_answer_output
78

89

910
def is_even_game(name):
@@ -12,19 +13,16 @@ def is_even_game(name):
1213
sleep(2)
1314

1415
correct_guesses = 0
15-
16+
1617
while correct_guesses < 3:
1718

1819
number = randint(1, 10000)
1920
answer = string(f'Question: {number}\nYour answer: ')
2021
correct_answer = 'no' if number % 2 else 'yes'
2122

2223
if answer != correct_answer:
23-
print(
24-
f"'{answer}' is wrong answer ;(. "
25-
f"Correct answer was '{correct_answer}'.\n"
26-
f"Let's try again, {name}!\n\n\n"
27-
)
24+
wrong_answer_output(answer, correct_answer, name)
25+
2826
sleep(2)
2927

3028
correct_guesses = 0
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
from brain_games.cli import welcome_user
2-
from brain_games.scripts.brain_even import is_even_game
2+
from brain_games.scripts.games.brain_even import is_even_game
3+
from brain_games.scripts.games.brain_calc import calc_game
34

45

56
def main():
67
name = welcome_user()
78
is_even_game(name)
9+
calc_game(name)
810

911

1012
if __name__ == "__main__":

brain_games/wrong_answer_output.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
def wrong_answer_output(answer, correct_answer, name):
2+
3+
if answer != correct_answer:
4+
print(
5+
f"'{answer}' is wrong answer ;(. "
6+
f"Correct answer was '{correct_answer}'.\n"
7+
f"Let's try again, {name}!\n\n\n"
8+
)
9+
10+
return

demo2.cast

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
{"version": 2, "width": 120, "height": 30, "timestamp": 1741886296, "env": {"SHELL": "/bin/bash", "TERM": "xterm-256color"}}
2+
[0.020568, "o", "\u001b[?2004h\u001b]0;moodeng@DESKTOP: ~/code/hexlet/python_full_course/python-project-49\u0007\u001b[01;32mmoodeng@DESKTOP\u001b[00m:\u001b[01;34m~/code/hexlet/python_full_course/python-project-49\u001b[00m$ "]
3+
[1.667878, "o", "u"]
4+
[1.867789, "o", "b"]
5+
[1.963746, "o", " "]
6+
[2.483922, "o", "\b\u001b[K"]
7+
[2.620779, "o", "\b\u001b[K"]
8+
[2.674703, "o", "v"]
9+
[2.743764, "o", " "]
10+
[3.331896, "o", "r"]
11+
[3.540712, "o", "u"]
12+
[3.768739, "o", "n"]
13+
[4.04272, "o", " "]
14+
[5.186761, "o", "b"]
15+
[5.286947, "o", "r"]
16+
[5.38681, "o", "a"]
17+
[5.614771, "o", "i"]
18+
[5.787808, "o", "n"]
19+
[6.220815, "o", "-"]
20+
[7.752788, "o", "v"]
21+
[7.756557, "o", "c"]
22+
[8.207788, "o", "\b\u001b[K"]
23+
[8.376928, "o", "\b\u001b[K"]
24+
[8.472791, "o", "c"]
25+
[8.604959, "o", "a"]
26+
[8.8188, "o", "l"]
27+
[9.233782, "o", "c"]
28+
[9.438867, "o", "\r\n\u001b[?2004l\r"]
29+
[9.514979, "o", "Welcome to the Brain Games!\r\nMay I have your name? "]
30+
[11.211782, "o", "B"]
31+
[11.376778, "o", "o"]
32+
[11.448643, "o", "b"]
33+
[11.686867, "o", "\r\nHello, Bob!\r\n\r\n"]
34+
[12.687436, "o", "What is the result of the expression?\r\n"]
35+
[13.688551, "o", "Question: 19 * 13\r\nYour answer: "]
36+
[20.766844, "o", "h"]
37+
[20.975704, "o", "z"]
38+
[21.335852, "o", "\r\n'hz' is wrong answer ;(. Correct answer was '247'.\r\nLet's try again, Bob!\r\n\r\n\r\n\r\n"]
39+
[23.336226, "o", "Question: 37 * 18\r\nYour answer: "]
40+
[24.991862, "o", "h"]
41+
[25.119026, "o", "z"]
42+
[27.248823, "o", "\b \b"]
43+
[27.430891, "o", "\b \b"]
44+
[29.627737, "o", "h"]
45+
[29.76222, "o", "z"]
46+
[29.898155, "o", "\r\n'hz' is wrong answer ;(. Correct answer was '666'.\r\nLet's try again, Bob!\r\n\r\n\r\n\r\n"]
47+
[31.898984, "o", "Question: 19 * 6\r\nYour answer: "]
48+
[38.664857, "o", "1"]
49+
[39.092772, "o", "2"]
50+
[39.503923, "o", "4"]
51+
[39.804219, "o", "\r\n'124' is wrong answer ;(. Correct answer was '114'.\r\nLet's try again, Bob!\r\n\r\n\r\n\r\n"]
52+
[41.805193, "o", "Question: 48 * 5\r\nYour answer: "]
53+
[52.284432, "o", "2"]
54+
[52.538693, "o", "4"]
55+
[52.825672, "o", "0"]
56+
[53.053228, "o", "\r\nCorrect!\r\n\r\n"]
57+
[54.05377, "o", "Question: 90 - 10\r\nYour answer: "]
58+
[55.691776, "o", "8"]
59+
[55.787801, "o", "0"]
60+
[56.089681, "o", "\r\nCorrect!\r\n\r\n"]
61+
[57.090275, "o", "Question: 20 + 17\r\nYour answer: "]
62+
[60.447716, "o", "3"]
63+
[60.902855, "o", "7"]
64+
[61.034958, "o", "\r\nCorrect!\r\n\r\n"]
65+
[62.036141, "o", "Congratulations, Bob!\r\n\r\n"]
66+
[62.042365, "o", "\u001b[?2004h\u001b]0;moodeng@DESKTOP: ~/code/hexlet/python_full_course/python-project-49\u0007\u001b[01;32mmoodeng@DESKTOP\u001b[00m:\u001b[01;34m~/code/hexlet/python_full_course/python-project-49\u001b[00m$ "]
67+
[65.030661, "o", "\u001b[?2004l\r\r\nexit\r\n"]

0 commit comments

Comments
 (0)