Creating a numerical question in a quiz

Jump to solution
bmorris24
Community Member

Hi all,

I am currently trying to create a numerical question in a quiz using Python and the Canvas API. However, it seems that (no matter the numerical_answer_type) the answer values are always set to 0. Here is the code I am using:

 

import requests
import json

header = {'Authorization': f'Bearer {access_token}'}
course_id = 123
quiz_id = 456
base_url = f'https://canvas.instructure.com/api/v1'

new_question = {
  'question_type': 'numerical_question',
  'points_possible': 1,
  'question_text': '<p>Give me a number.</p>',
  'answers': [
    {
      'start': 10.0,
      'end': 15.0,
      'numerical_answer_type': 'range_answer'
    },
    {
      'exact': 75.0,
      'margin': 25.0,
      'numerical_answer_type': 'exact_answer'
    },
    {
      'approximate': 1000.0,
      'precision': 5.0,
      'numerical_answer_type': 'precision_answer'
    }
  ]
}
canvas_json = {'question': new_question}
endpoint = f'{base_url}/courses/{course_id}/quizzes/{quiz_id}/questions'
resp = requests.post(endpoint, json=canvas_json, headers=header)
print(resp)
print(json.dumps(resp.json(), indent=2))

 

The response that I get back is exactly as expected, except that start, end, exact, margin, approximate, and precision are all 0, and is reflected as such on Canvas. I can manually update these values on Canvas, and GET-ing the question from there yields the correct values. What am I doing wrong and how do I properly create numerical questions through the API?

P.S. I've found a similar post from four years ago but no one seems to have answered it.

Labels (5)
0 Likes
1 Solution
James
Community Champion

@bmorris24 

Several responses were provided in that other thread, including mine about opening up the developer tools to see what Canvas sends. It's just that no one marked it as answered, but there is some good stuff there, including some people saying that they were able to figure it out.

I just now added a question and watched what Canvas sent using the browser's developer tools It sends a non-API request, but often the API requests work the same way.

When you look at the response, what Canvas is not using the short names like start, end, exact, margin, approximate, or precision. They have longer names like answer_range_start. This sometimes happens that the object you're looking at with a view is not the same as the object you have to send with a POST or PUT.

Here are the key results from the request I made. It isn't in JSON format, but you can at least see the names. 

question[answers][0][answer_range_start]: 10
question[answers][0][answer_range_end]: 15
question[answers][0][answer_weight]: 100
question[answers][0][numerical_answer_type]: range_answer
question[answers][1][answer_exact]: 75
question[answers][1][answer_error_margin]: 25
question[answers][1][answer_weight]: 100
question[answers][1][numerical_answer_type]: exact_answer
question[answers][2][answer_approximate]: 1000
question[answers][2][answer_precision]: 5
question[answers][2][answer_weight]: 100
question[answers][2][numerical_answer_type]: precision_answer

 

The question remains whether the API will accept the answer_ property. I am not at a spot where I can try this right now, but try modifying your request to use the names that Canvas uses and see if that fixes it.

 

View solution in original post

0 Likes