I'm working on a code-checker for my students (I tutor). The project is that they write a function that prints a specific string using the print
keyword. I want to be able to test what they have printed by storing it and matching to a list (or something similar). The basic setup is:
def checker():
#run user code
do some other things like save and check error messages etc
Now somewhere in this checker
function I want to be able to keep track of what was printed. In Javascript, I was able to do something like:
var logs = [];
var hold_logger = console.log //saves the console.log so nothing gets ruined
console.log = function (x) { logs.push(x) };
Now when I run the students code, instead of printing to the console, it pushes the value to logs
. I want to achieve the same thing in Python 2.7.