Python¶
Python is currently available only in courses using the tile view layout, such as OMB+ and HM4mint. If you require Python access in the pool, feel free to get in touch with us.
Python in problems¶
pythonevaluator¶
Checks the student's code.
\begin{pythonevaluator}
for i in range(1,50):
if(expected_sum(i) != sum(i)):
exit(11) # returns with grading code 11
exit(0) # returns with grading code 0
\end{pythonevaluator}
== or != for integers.For floats, use
np.isclose or np.allclose instead - exact comparison rejects correct answers due to rounding.
pythonfunction¶
Creates an author defined Python function which can be used by the student as well as in pythonevaluator.
\begin{pythonfunction}{expected_sum} # expected_sum is the name of the function
def expected_sum(n):
s = 0
for i in range(1,n+1):
s += i
return s
\end{pythonfunction}
pythonsolution¶
Creates a sample solution for the code fragment with the given name
\begin{pythonsolution}{sum}
def sum(n):
s = 0
for i in range(1,n):
s += i
return s
\end{pythonsolution}
pythonanswer¶
Creates an input field for a code fragment. \pythonanswer[template]{name}
templatecan be used to define a template for the answer.nameis the identifier of the code fragment.
\pythonanswer[
def sum(n):
s = \editable # creates an editable field for the student
for i in range(1,n+1):
\multilineEditable # creates an editable field for the student, which allows line breaks
return s
]{sum}
include¶
Defines an include for a whole Python problem or a question of it \include{lid}
lidis the lid of the Python script that is meant to be included
includeAnswer¶
Defines an include of an answer for a question in a Python problem \includeAnswer[forced]{sum}. The argument forced can be used to configure consequential errors. In case the user answered the answer's question wrong, the sample solution is included. If the argument forced is set, the user's answer is included, even if the answer's question is wrong.
forcedthe user answer is included, even if the corresponding question is answered wrongsumis the name of the pythonanswer that is supposed to be included
pythongrading¶
Specifies the grade and feedback for a certain evaluator exit code. \pythongrading{exitcode}{grade}{feedback}
exitcodeis the exit valuegradethe grade value (number between 0 and 1)feedbackfeedback that is displayed to the user in the correction view
\pythongrading{0}{1.0}{All tests ran and passed}
\pythongrading{10}{0.0}{This is not a 3x3 matrix}
\pythongrading{11}{0.0}{At least one element is zero}
\pythongrading{12}{0.0}{The determinant is not zero}
exit(n) explicitly for every case, and define \pythongrading{0}{...} yourself.An evaluator path without an explicit
exit(n) silently gets a grade of 1.0.
Python scripts¶
For the sake of reusability and separation of concerns, you can create Python scripts in WebMiau which you can then include in Python problems with the include command. Python scripts are just pure Python code.
Python in articles¶
pythoninput¶
Creates an input field for Python code
\begin{pythoninput}
a11 = 1/4; # is the initial content of the field. It can be edited by the student
a12 = 2/3;
a21 = -1/5;
a22 = 3/5;
A = [ a11, a12; a21, a22 ];
A
\end{pythoninput}
pythoninput¶
Creates an input field for Python code \pythoninput
pythonbutton¶
Creates a button that sends the contents of a Python input field to a Python process and
displays the result in a Python output field
\pythonbutton
pythonoutput¶
Creates a text field for output from Python \pythonoutput
WebMiau Example¶
Python modules (libraries)¶
To find out the available python modules type into a test environment of a python problem the code: help('modules'). The result is a list of the available modules, including the Python Standard Library. Here you find details about the Python Standard Library