AnsweredAssumed Answered

How do I add answers when creating a quiz using the API?

Question asked by Jeff Bulmer on Jul 15, 2019
Latest reply on Jul 29, 2019 by Jeff Bulmer

Hi,

 

I'm using the canvasapi Python module, and I'm trying to create quizzes from a file. I am able to create the entire quiz this way except for adding answers. I've looked everywhere I can think of for information on what I'm doing wrong with the answer objects, but I'm unable to find even the basic format of an answer object. I've attached some sample code below from one of my hard-coded quizzes that should illustrate the issue I'm having:

 

quiz.create_question(question={
    'question_name': 'Gender',
    'question_type': 'multiple_choice_question',
    'question_text': 'Which gender do you identify as?',
    'points_possible': '0.0',
    'correct_comments': '',
    'incorrect_comments': '',
    'neutral_comments': '',
    'correct_comments_html': '',
    'incorrect_comments_html': '',
    'neutral_comments_html': '',
    'answers': [{'answer_text':'Male', 'weight':100.0}
                ,{'answer_text':'Female', 'weight':0.0}
                ,{'answer_text':'Other', 'weight':0.0}
                ,{'answer_text':'Prefer not to say', 'weight':0.0}
                ]
})

 

I had been under the impression that to add an answer, I should only need to specify the answer_text and the weight (or answer_weight, I've tried both and neither's worked). However, when I run that (in terminal), I get this:

 

Traceback (most recent call last):
  File "<stdin>", line 15, in <module>
  File "/home/jeff/anaconda3/lib/python3.6/site-packages/canvasapi/quiz.py", line 136, in create_question
    _kwargs=combine_kwargs(**kwargs)
  File "/home/jeff/anaconda3/lib/python3.6/site-packages/canvasapi/requester.py", line 111, in request
    raise CanvasException("API encountered an error processing your request")
canvasapi.exceptions.CanvasException: API encountered an error processing your request

 

Obviously that error message isn't particularly helpful.
Out of curiosity, I went onto the website and created an answer via the web interface, just to see what the structure of an answer is. This is what I got (just the answers section, because this question is already getting pretty long):

QuizQuestion(_requester=<canvasapi.requester.Requester object at 0x7f5ac4bd47b8>, id=1337341, quiz_id=122827, quiz_group_id=None, assessment_question_id=1851679, position=None, question_name=Question1, question_type=multiple_choice_question, question_text=<p>It worked!</p>, points_possible=1.0, correct_comments=, incorrect_comments=, neutral_comments=, correct_comments_html=, incorrect_comments_html=, neutral_comments_html=, answers=[{'id': 505, 'text': '1', 'html': '', 'comments': '', 'comments_html': '', 'weight': 100.0}], variables=None, formulas=None, answer_tolerance=None, formula_decimal_places=None, matches=None, matching_answer_incorrect_matches=None, course_id=31084)

I've tried specifying each of those fields within the python code to create my own answers, but again this didn't work. Even if it had, the 'id' field itself already presents a problem, since it's going to be very difficult for me to manually ensure I'm always getting the next unique id.

That said, the structure of the Answer object kind of indicates to me that there's probably a way to actually make this, I just haven't been able to find it in the documentation at all. Has anyone encountered this problem before, and how did you deal with it?


Outcomes