Test for Creation & Use of Variable

Test for Creation & Use of Variable

de către Michael Backus-
Număr de răspunsuri: 1

I'm having a little trouble wrapping my head around the best way to assess the following question:

Write a program that will light up the blue LED. Use a variable [called blueLED] so that if you decide to hook up the LED to a different pin, you can quickly change your code to match.

Ideally I would not have to include the test inside the square brackets, but I suspect that will make creating this question much easier.

To provide context, here's a picture of the circuit with an ideal programming solution:


I already know how to assess whether students are able to write a program that will light up the blue LED, but I don't know the best way to go about making sure they created a variable and used it.

For example, the following code would still pass my test:

void setup()
{
  pinMode(4,OUTPUT);
  digitalWrite(4,HIGH);
}

void loop()
{
}

I know I can turn it into a cpp_via_python question (or in my case, an arduino_via_python question), and alter the template to check for a blueLED string, but finding blueLED does not necessarily mean that it was used correctly. For example, the following would light up the LED and pass a string search test for blueLED:

int blueLED = 4;

void setup()
{
  pinMode(blueLED,OUTPUT);
  digitalWrite(4,HIGH);
}

void loop()
{
}

I'm thinking that maybe a better way to handle this would be to alter the setup so that the first line assigns different values to blueLED and see if I get the expected output. For example:

void setup()
{
  blueLED = {{testcases}}
  ...
}

I could then check for the expected output. The problem is, I don't know how to hijack the students' setup so that it will run that additional line of code.

I'm not sure my approach is the best. I'm open to any solution that gets my students writing readable and maintainable code.

Ca răspuns la Michael Backus

Re: Test for Creation & Use of Variable

de către Richard Lobb-

Assuming you're using some sort of C++-via-Python question type, you have the student's code available in a Python variable, after a statement of the form

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

You can then edit that string in any way you choose before writing it to a file and compiling and running it. For example you could use a regular expressions like "blueLED *= *(\d+)". You should first check that there is exactly one match, then use (say) re.sub() to replace the digit string with a per-test-case number extracted from the test case, e.g. like

pin_num = {{ TEST.extra }}

However, this is all quite a lot of work for one question. It would be much easier to tell the student that they must use a globally-defined variable blueLED (which of course they then shouldn't define themselves). You could define that global variable differently in each test.