@@ -105,7 +105,12 @@ def answer_next_question():
105105
106106 data  =  request .get_json ()
107107 text  =  data ["text" ]
108-  correct  =  str_cmp (text .lower (), q .answer .lower ())
108+ 
109+  try :
110+  correct  =  str_cmp (text .casefold ().strip (), q .answer .lower ())
111+  except  TypeError :
112+  return  jsonify (status = "success" , correct = False )
113+ 
109114
110115 ans  =  Answer .query .filter_by (user_id = user .id , question_id = q .id ).first ()
111116
@@ -199,6 +204,10 @@ def answer_eval():
199204 return  jsonify (status = "error" ,
200205 reason = "missing 'language' property in JSON body" ), 400 
201206
207+  if  language  not  in "js" , "python" ):
208+  return  jsonify (status = "error" ,
209+  reason = "unsupported language. valid choices are 'js' or 'python'" ), 400 
210+ 
202211 # designated output variable for evaluation 
203212 if  language  ==  "js" :
204213 code  +=  ";output" 
@@ -220,13 +229,20 @@ def answer_eval():
220229 eval_error  =  eval_data ["error" ]
221230 eval_output  =  str (eval_data ["output" ])
222231
232+  if  language  ==  "python" :
233+  eval_output  =  eval_output .rstrip () # remove trailing \n from print() 
234+ 
223235 # any API error is an automatic failure 
224236 if  eval_error :
225237 return  jsonify (status = "success" ,
226238 correct = False ,
227239 js_error = eval_error )
228240
229-  correct  =  str_cmp (eval_output , q .answer )
241+  try :
242+  correct  =  str_cmp (eval_output , q .answer )
243+  except  TypeError :
244+  return  jsonify (correct = False ,
245+  status = "success" )
230246
231247 if  request .json .get ("checkOnly" , False ):
232248 return  jsonify (correct = correct ,
0 commit comments