Where to put variables for a simple one-liner-test in Python?

Where to put variables for a simple one-liner-test in Python?

de către Christian Kruggel-
Număr de răspunsuri: 4

Hi all,

I'm trying to set up a Python-CodeRunner that does not ask students to write down an entire function but to give a single line of code instead. That one line ought change a dictionary thus the dictionary needs to be declared somehow somewhere in advance. I thought the template parms might be the best place to do so and put some JSON there:

{ "noten" : [2.0, 3.0, 1.0] }

I consider this to be the equivalent to Python's

noten = [2.0, 3.0, 1.0]

So I expected to first test

(len(noten) == 2) & (noten[0] == 3.0) & (noten[1] == 1.0)

to come up with True or False but got

***Laufzeitfehler***
Traceback (most recent call last):
  File "__tester__.python3", line 1, in <module>
    noten.remove(2.0)
NameError: name 'noten' is not defined

Debug is

Run 1
noten.remove(2.0)
__student_answer__ = """noten.remove(2.0)"""
SEPARATOR = "#<ab@17943918#@>#"
(len(noten) == 2) & (noten[0] == 3.0) & (noten[1] == 1.0)

Providing the list along with the test case itself doesn't work neither. Being a bloody newbie I'm afraid of missing something very basic ....

Where can I declare the list?

Many thanks in advance

Christian

Ca răspuns la Christian Kruggel

Re: Where to put variables for a simple one-liner-test in Python?

de către Christian Kruggel-

Init list noten right before {{ STUDENT_ANSWER }} in the tempelate

OK, initializing the variable right before {{ STUDEN_ANSWER }} is the right place to put that line. Bad idea to test some void method such as remove this way ;-)


Ca răspuns la Christian Kruggel

Re: Where to put variables for a simple one-liner-test in Python?

de către Richard Lobb-

I think you probably need to read through the documentation, perhaps starting with the section on the architecture of CodeRunner: see here. Or you could watch the YouTube videos.

The default Python3 question type builds a program consisting of the student-supplied code followed by the test code. You are trying to insert some of your own code before the student code and then have the test code following that. To achieve that you need to use your own customised template or (when you get more familiar with CodeRunner) your own question type.

Template parameters are a way for question authors to write more general templates. For example, if the template had contained the line

test_list = {{ noten }}

your template parameter would have defined a list test_list in the way you want. But since the standard Python3 template contains no such code, your parameter achieved nothing. I think you can forget about template parameters until you've started writing your own question types.

I suggest after reading the documentation you experiment with changing the template by clicking the Customise checkbox and seeing if you can find a place to insert an extra line of code, such as

noten = [2.0, 3.0, 1.0]

to achieve the result you want.

You might also be interested in the new "gap filler" question type: see here.

Ca răspuns la Richard Lobb

Re: Where to put variables for a simple one-liner-test in Python?

de către Christian Kruggel-
Hello Richard,

Thank you for your answer. I read „The Architecture of CodeRunner“ but haven‘t really figured out what output the grader uses. On the one hand I have the student‘s code producing some output. On the other hand each test has its own output.

In my situation (modifying a list) the removal of an element does not yield any output. I set up a single test that ought run after the student‘s void code changed the list. That test is a sequence of ifs testing the list and complaining about it if it does not fit requirements or remain silent if the modification left the list as expected.

Funny enough: If the student‘s answer is some hollow statement with no output at all (print('')) the test passes – though it should give some line after the hollow predecessor did do nothing to the now well known list noten. So I‘d like to know where I can have a detailled look in the test‘s progress. It seems that the test itself does not really execute all code I put into it.

Is there a brief word to clarify this?

Christian
Ca răspuns la Christian Kruggel

Re: Where to put variables for a simple one-liner-test in Python?

de către Christian Kruggel-

Sry, don't want to wast your time. Debugging is enabled indicating non of the tests being executed:

Test not executed?

Silly me! Splitting the different ifs into individual tests does the trick!

Split tests as far as possible!

Great software, many thanks!