New and Getting Unexpected Indentation Error

New and Getting Unexpected Indentation Error

by Brad Strassburger -
Number of replies: 8

My apologies as I am brand new to this, but it seems so amazing I really want to use this with my students. I am working through the learning process but have run into a weird situation. All I want to do is have students write the following code

varX = 5

varY = 10

if varX > varY:

    print ("Hello")

elif varX < varY:

    print ("Goodbye")

else:

    print ("Equal")

However it keeps saying that there is an indentation error

"Sorry: IndentationError: expected an indented block after 'if' statement on line 23 (__tester__.python3, line 24)"

but there simply isn't. I am really not sure what I am doing wrong and would really appreciate some help

In reply to Brad Strassburger

Re: New and Getting Unexpected Indentation Error

by Richard Lobb -
What question type are you using, and has the question been customised? I don't think either the standard python3 or python3_w_input question types could give rise to that error unless the template has been customised: the word Sorry doesn't exist in the CodeRunner codebase, at least not in that sort of context. 

The program that's actually run is defined by the question template. The fact that you're getting an error on line 23 when there aren't that many lines in the student answer suggests that there's an error in the template. To see what's going on, click the Template debugging checkbox, turn off Validate on save, save the question and preview it Then you'll get shown exactly what code is being run. Hopefully the cause will then become clear. If not, please export export the question as an xml file and post back with the exported question attached.
In reply to Richard Lobb

Re: New and Getting Unexpected Indentation Error

by Brad Strassburger -
Thanks for getting back to me.
I am using the Python3 question type. I have looked at the template but honestly do not understand it. I have checked the Template Debugging and turned off the Validate on Save to test the program and it still gives me the exact same error to the student response.
I have exported the question (but I believe it is all four of my sample questions). the one that is giving me trouble is title "if statement". I am not sure how to open the file so I am not sure that I have done it correctly.
In reply to Brad Strassburger

Re: New and Getting Unexpected Indentation Error

by Brad Strassburger -
This is confusing. I can see from the template debugging that it is the processing that is not indenting, not my code. I have changed the template with a simple indent on line 10
{{ STUDENT_ANSWER }}

__student_answer__ = """{{ STUDENT_ANSWER | e('py') }}"""

SEPARATOR = "#<ab@17943918#@>#"

{% for TEST in TESTCASES %}
{{ TEST.testcode }}
{% if not loop.last %}
    print(SEPARATOR)
{% endif %}
{% endfor %}

It now seems to work except for the else statement. Now it is saying there is no indent after the else statement. Is there a way to modify the template to make it see the else and add an indent?
In reply to Brad Strassburger

Re: New and Getting Unexpected Indentation Error

by Richard Lobb -

Thanks for submitting the exported questions. I now see the problem.

The default Python3 question type essentially runs a program consisting of the student answer followed by your test code. It does this in a loop for each test case, but let's disregard the loop aspect for now and just consider each test separately.

Your tests are just isolated lines like if varx > vary: so the program finishes with an incomplete if statement, causing the error you're seeing.

The question you're trying to ask doesn't fit the standard template. You ask the students to define two values varX and varY and then to compare them. But if the student has defined the values of the variables already, your test code - which runs after the student code - has no control over the values. So you can't test the different outcomes.

There are several ways you might go about asking your question.

  1. Ask students to write a function, say compare_values(var_x, var_y), that compares two given values and prints carefully specified messages for the different cases.
    • This makes it easy to test students' code because you're providing them with both the names and the values of the variables to use.
    • This is our preferred approach, but it does mean you need to introduce functions very early in the course.
      • Students don't need to understand how function works at this stage - they just need to be shown the syntax.
  2. Ask students to use the input() function to read two integer values from standard input and print appropriate messages.
    • That gets you into the annoying sidetrack of specifying the prompts for the input - a simple solution is not to use prompts at all.
    • If using prompts, consider use of the python3_w_input question type instead.
  3. Ask the students to write a code snippet that tests the values of two values var_x and var_y that you provide in the test case and prints appropriate messages.
    • The test code is now two assignment statements to set the values of the two variables.
    • This requires a different template in which the test code precedes the student answer. For example, you might customise the question, turn off the is_combinator checkbox and set the template to just
      {{ TEST.testcode }}
      {{ STUDENT_ANSWER }}
                          

You can also write questions in which the template does clever things like finding the values the student has used in their code for the two variables, replacing them with values that you provide in the test code. But I suggest for a start that you stick to one of the above three ways.

In reply to Richard Lobb

Re: New and Getting Unexpected Indentation Error

by Brad Strassburger -
So I tried all three options and I am still getting the same error.
Sorry: IndentationError: expected an indented block after 'if' statement on line 17 (__tester__.python3, line 18)
The line number changes based on the code, but it is still the same error.
In reply to Brad Strassburger

Re: New and Getting Unexpected Indentation Error

by Henry Hickman -
Based on Richard's three definitions, I've created three example questions that compare two variables.
FunctionCompare asks students to write a function that does this.
InputCompare requires them to input two numbers before doing the comparison.
TemplateCompare defines the variables in the test cases, as the template has been modified to allow this.

All approaches have merit, and it depends on your students and what you aim to teach to figure out which is best.
I currently design for a curriculum very focused on input and output, so I'd use InputCompare, but also the input function is rarely used in real world programming, so if that's your goal then FunctionCompare is probably the best bet (as functions are very real world programming).
TemplateCompare is neat, but as far as I can tell is only necessary if you need students to name their variables a certain way, which you may, but I've never had that need. Having students come up with their own variable names (and learning the hard way that choosing bad variable names is a bad idea) is part of the fun of teaching programming to me! (Note this isn't to say template customization is worthless, quite the opposite I use it a lot, but in this particular use case I don't see it as necessary).

Let me know if you have any questions about how these work.

Kind regards,
Henry
In reply to Henry Hickman

Re: New and Getting Unexpected Indentation Error

by Richard Lobb -
For more examples of Python CodeRunner questions, check out the demo page: https://coderunner.org.nz/mod/page/view.php?id=549. All the questions on that page are included in the samples folder in the CodeRunner repository at https://github.com/trampgeek/CodeRunner.
In reply to Richard Lobb

Re: New and Getting Unexpected Indentation Error

by Brad Strassburger -
I want to thank you (Richard and Harry) now that I have figured out how to import things properly, I have a much better idea of how to get things to work. I have to admit that there is some complexity to the program that I beyond me (I am just a high school teacher) this does add some ability for me to use the tool.
It is a very innovative tool with a lot of possibilities, and I will continue to work through it as best I can. Again I really appreciate your help.