-
Notifications
You must be signed in to change notification settings - Fork 0
/
exercise_utils.py
52 lines (40 loc) · 1.65 KB
/
exercise_utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
from IPython.display import display, Markdown, HTML
import textwrap
html_disp_style = '<p style="color:{c};font-size:20px;">{txt}</p>'
class Question(object):
def __init__(self,
correct_answer,
fail_message, success_message,
#
hint = None,
func2change_val = None
):
self.hinttxt = hint
self.answer = correct_answer
self.success_message = success_message
self.fail_message = fail_message
if not func2change_val:
self.func2change_val = lambda x:x
else:
self.func2change_val = func2change_val
def check(self, val2check):
if func2change_val(val2check) == func2change_val(correct_answer):
display(HTML(html_disp_style.format(c='green', txt= 'Success!')))
display(Markdown(textwrap.indent(self.success_message,'> ')))
else:
display(HTML(html_disp_style.format(c='red', txt= 'Nope!')))
display(Markdown(textwrap.indent(self.fail_message,'> ')))
def hint(self):
if self.hinttxt:
display(HTML(
html_disp_style.format(c='DarkGoldenRod', txt= 'Hint:')
)
)
display(Markdown(textwrap.indent(self.hinttxt,'')))
else:
print('No hint available!')
class NoSolveQuestion(Question):
def __init__(self, hint):
super().__init__(None,None,None,hint=hint)
def check(self, val2check):
raise AttributeError('"Check" is not implemented for this question')