1
1
from typing import Literal
2
2
3
+ import numpy as np
3
4
import panel as pn
4
5
5
6
import coastal_dynamics as cd
@@ -11,24 +12,24 @@ class NumericQuestion(Question):
11
12
A class to create and manage a numeric answer question widget.
12
13
13
14
This class creates a numeric question using Panel widgets.
14
- It supports a question text, numeric answer, and precision for the answer.
15
+ It supports a question text, numeric answer, and sig_figs for the answer.
15
16
16
17
Attributes:
17
18
correct_answer (float): The correct numeric answer.
18
- precision (int): The precision of the numeric answer.
19
+ sig_figs (int): The number of significant figures of the numeric answer.
19
20
answer_input (pn.widgets.FloatInput): The widget for inputting the answer.
20
21
"""
21
22
22
23
def __init__ (
23
24
self ,
24
25
question_name : str ,
25
26
question_text : str ,
26
- question_answer : float ,
27
+ question_answer : str ,
27
28
question_feedback : dict [Literal ["correct" , "incorrect" ], str ],
28
- precision : int | None = None ,
29
+ sig_figs : int | None = None ,
29
30
):
30
31
self .correct_answer = question_answer
31
- self .precision = precision
32
+ self .sig_figs = sig_figs
32
33
self .answer_input : pn .widgets .FloatInput
33
34
34
35
super ().__init__ (question_name , question_text , question_feedback )
@@ -43,12 +44,26 @@ def check_answer(self, event) -> None:
43
44
"""Check the submitted answer against the correct answer."""
44
45
try :
45
46
user_answer = float (self .answer_input .value )
46
- if self .precision :
47
- user_answer = round (user_answer , self .precision )
48
- if self .hash_answer (user_answer , "numeric" ) == self .correct_answer :
47
+
48
+ if self .sig_figs :
49
+ user_answer = np .format_float_positional (
50
+ user_answer ,
51
+ precision = self .sig_figs ,
52
+ unique = False ,
53
+ fractional = False ,
54
+ trim = "k" ,
55
+ )
56
+
57
+ hashed_user_answer = self .hash_answer (
58
+ user_answer , "numeric" , sig_figs = self .sig_figs
59
+ )
60
+
61
+ if hashed_user_answer == self .correct_answer :
49
62
self .feedback_widget .value = self .feedback ["correct" ]
63
+
50
64
else :
51
65
self .feedback_widget .value = self .feedback ["incorrect" ]
66
+
52
67
except ValueError :
53
68
self .feedback_widget .value = "Please enter a valid number."
54
69
@@ -65,21 +80,28 @@ def serve(self) -> pn.Column:
65
80
if __name__ == "__main__" :
66
81
question_data = {
67
82
"question" : "What is the relative importance of S2 vs M2?" ,
68
- "answer" : 0.33 ,
69
- "precision " : 2 ,
83
+ "answer" : 35 ,
84
+ "sig_figs " : 2 ,
70
85
}
71
86
72
87
nq = NumericQuestion (
73
88
question_name = "Q3: Simple numeric question" ,
74
89
question_text = question_data ["question" ],
75
90
question_answer = cd .hash_answer (
76
- round (float (question_data ["answer" ]), question_data ["precision" ]), "numeric"
91
+ np .format_float_positional (
92
+ float (question_data ["answer" ]),
93
+ precision = question_data ["sig_figs" ],
94
+ unique = False ,
95
+ fractional = False ,
96
+ trim = "k" ,
97
+ ),
98
+ "numeric" ,
77
99
),
78
100
question_feedback = {
79
101
"correct" : "Correct!..." ,
80
102
"incorrect" : "Incorrect, try again. Please consider that..." ,
81
103
},
82
- precision = question_data ["precision " ],
104
+ sig_figs = question_data ["sig_figs " ],
83
105
)
84
106
nq .serve ().show ()
85
107
print ("done" )
0 commit comments