You're not reading the latest revision of this page, which is here.

Python

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}

pythonfunction

Creates an author defined Python function.

\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{pythonsosultion}

pythonanswer

Creates an input field for a code fragment. \pythonanswer[template]{name}

  • template can be used to define a template for the answer.
  • name is 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}

  • lid is 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.

  • forced the user answer is included, even if the corresponding question is answered wrong
  • sum is 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}

  • exitcode is the exit value
  • grade the grade value (number between 0 and 1)
  • feedback feedback 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}

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}{60}{20}               # 60: the size of the field in columns # 20: the size of the field in rows
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{width}{height}

  • width the size of the field in columns
  • height the size of the field in rows

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[label]

  • label is the text on the button

pythonoutput

Creates a text field for output from Python \pythonoutput{width}{height}

  • width the size of the field in columns
  • height the size of the field in rows

WebMiau Example

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