Question Authors' Forum

Type of output - Python

 
Picture of Yess U
Type of output - Python
by Yess U - Thursday, 27 September 2018, 7:56 PM
 

Hi!

I'm doing some Python questions where students should print different types of data. I have a problem when they have to print numbers, the outputs are being compared as strings, so when I qualify the exercise, it tells me that 1 is different than 1.0 when in fact I should mark the answer as correct.

Is there any way to compare the outputs as numbers and not as strings?


Thank you so much for your help

Picture of Richard Lobb
Re: Type of output - Python
by Richard Lobb - Thursday, 27 September 2018, 9:31 PM
 

If you're asking questions where the students have to print numbers, then you're fundamentally stuck with having to compare text. There are ways to improve the comparison but none are exactly trivial. The example above could be handled by using a regular expression grader rather than an exact match grader, e.g. with a regular expression like 3(\.0+)?. But exposing regular expressions to the student's view in the Expected column causes confusion unless you're prepared to put the time into explaining what they mean.

With a lot more effort you can write your own custom template grader that collects the student's output in some way (e.g. via a Python subprocess), extracts the numbers and checks if they match the required numbers to some tolerance.

One of my colleagues goes even further. She has a Matlab question type that runs the sample answer, extracts all the numbers from it, then runs the student's code and extracts the numbers from that, and finally compares the two. In this way she can print out very informative feedback and the same question type can be used for a large range of problems.

Personally I try to avoid the whole issue by posing questions in a different way. For example, "Write a program that computes ... and prints the answer with exactly 2 digits of precision after the decimal point."  [Even this can be problematic in certain cases; you need to be a bit careful in your choice of test data.]

Even better: use write a function questions, where the student function returns a numeric value. Then you can just write tests like:

expected_ans = 3.14159
if abs(student_func(input_data) - expected_ans) > 0.0001:
    print("Correct")
else:
    print("Error")

where the Expected output is the word Correct. Other techniques are possible by using the Extra field of the test data to hold hidden test code, perhaps hiding some of the standard columns in the result table. Have fun exploring the space of possibilities :)

Richard

Picture of Yess U
Re: Type of output - Python
by Yess U - Friday, 28 September 2018, 5:34 AM
 

Thank you very much for answering me so fast. I like the possibilities you give to solve the problem, however, I have a question.

* In case the answers of the students are not with functions, but it is code that prints something and qualifies with "Expected output", how should the template be?

Picture of Yess U
Re: Type of output - Python
by Yess U - Friday, 28 September 2018, 6:15 AM
 

I have an additional question to the one above,

How can I hide the "Expected output" column and show other columns as "Extra template data"?

Picture of Richard Lobb
Re: Type of output - Python
by Richard Lobb - Friday, 28 September 2018, 10:42 AM
 

To see the template that's being used by any particular question type, just create a question of that type and click the Customise checkbox. For the case you quote of a python program that prints output to be compared with the expected output, a non-combinator template could be just

{{ STUDENT_ANSWER }}

which just runs the student code as is.

To control what columns are displayed (assuming you're using a standard built-in grader, not a template grader), again you need to customise the question. Then enter something suitable into the Result Columns field in the Customisation panel. On-line help is available.

BTW: you seem to be using Spanish questions but the CodeRunner output is in English. Most of the CodeRunner language strings are now available in Spanish, so you might want to install the Spanish language pack for Moodle and set the preferred language to Spanish.

Richard

Picture of Yess U
Re: Type of output - Python
by Yess U - Saturday, 29 September 2018, 1:00 PM
 
Thank you very much. Thanks for taking the time to help me. you're the best.