Help converting advanced problem to Python 3


RE: Python 3 upgrade

Looking for some help w/ code. I am not a programmer, however, with the move from Python 2 to Python 3, I need help with this script that will no longer work in version 3.

This code is a blank advanced problem type that allows a text response that can have data collected in reports. Is anyone willing to look at the code, test, and help me update it? I believe I got this original code from one of the older forums. I wish I could give credit to the individual.

Here’s the wonky error message:

“cannot create LoncapaProblem block-v1:USMx+Pythontest101+T32019+type@problem+block@947cb1178fbe45f9b1cc1f41450f495e: Error while executing script code: Couldn’t execute jailed code: stdout: b’’, stderr: b’Traceback (most recent call last):\n File “jailed_code”, line 19, in <module>\n exec(code, g_dict)\n File “<string>”, line 71\n response =;&#39;, ans)\n ^\nIndentationError: expected an indented block\n’ with status code: 1”


<problem markdown="null" max_attempts="5">
<div class="problem-description">
<!-- This is the problem description -->
<p>Let’s begin this section by exploring the topic of......</p>

<label>Please enter your response here:</label> <!-- You can change this text -->
<script type="loncapa/python">
The block below is the grading function for this problem.
The name you assign it here will be used below.

import re
def short_response(expect, ans):
response ='', ans)
if response:
return 1
return 0

<span id="short_response_question_1"/> <!-- Edit this id, for each problem -->
<script type="text/javascript">
/* The object here is to replace the single line input with a textarea */
(function() {
var elem = $("#short_response_question_1")
var label = $("#short_response_question_1")
label.attr('for', elem.attr('id'));
var description = $("#short_response_question_1")
description.attr('id', elem.attr('id')+'-description');
/* There's CSS in the LMS that controls the height, so we have to override here */
var textarea = $('<textarea style="height:150px" rows="20" cols="70"/>');

//This is just another way to do an iterator in JS
for (attrib in {'id':null, 'name':null}) {
textarea.attr(attrib, elem.attr(attrib));
textarea.attr('aria-describedby', elem.attr('id')+'-description');
/* copy over the submitted value and give an aria description*/
/* clear out echoed text */

<customresponse cfn="short_response"><!-- This 'cfn' name is the name of the python grading function above -->


(BTW, I moved and retitled this post, and edited the code so it would display.)

I’m not sure if the code here looks exactly as it does in your Studio. In Python, the indentation of each line is important. After the “def short_response” line, the next lines need to be indented, and the same after “if repsponse:”. Basically, every line that ends with a colon introduces a block that needs to be indented further. It should look like this:

import re
def short_response(expect, ans):
    response ='', ans)
    if response:
        return 1
        return 0
1 Like

Thank you so much Ned!
I just indented a few lines as you instructed and it works in the Python 3 sandbox.
I’ve already learned a simple very foundational Python lesson.
Obviously, if anyone needs an open text response code that works in edX - here it is.
Consider this closed.

Much appreciated,