Exercise sample: attached files

Change this sample according to your needs. Run all the cells, and upload resulting .xml file to Moodle.

Auxilary functions


In [3]:
%pylab inline
from moodle import *


Populating the interactive namespace from numpy and matplotlib

Question parameters

Generate parameters, that appear in the questions. We use seed for random generator.


In [8]:
import random

random.seed(123)

parameters = [random.randint(100,1000) for i in range(10)]

Question body

Write the function, that generates the text of the question. You can use the following syntax to add different inputs to question string q:

  • value of the variable: q = q + str(x)
  • expressions: q = q + str(1+2*x)
  • answer input field: q = q + (num_q(answer, precision))

Note on embedding files

We use <a name="data.csv" href="data:text/csv;base64,...> tag to embed arbitrary file.


In [9]:
import io
import base64
import numpy
import random

def question_text(parameter):
    seed =  parameter # parameter contains random seed
    random.seed(seed)
    mu = random.randint(10,100)
    sigma = random.randint(10,50)
    sequence = [random.gauss(mu,sigma) for x in range(random.randint(5000,6000))]
    string = "\n".join(["%f"% x for x in sequence])
    q = random.randint(1,99)
    data = base64.b64encode(string.encode('ascii')).decode()
    perc = numpy.percentile(sequence, q)
    q_text = """<p>Find %d-th percentile for data in the file
    <a name="data.csv" href="data:text/csv;base64,%s" />data.csv</a>.</p>
    What is %d-th percentile? %s
    """ % (q,data,q,num_q(perc,0.1))
    return q_text

In [10]:
# display the first question
from IPython.display import HTML
HTML(question_text(123))


Out[10]:

Find 27-th percentile for data in the file data.csv.

What is 27-th percentile? {1:NUMERICAL:=-0.357260:0.100000#Pravilno~-0.357260:1.000000#Premalo pravilnih decimalk}

Write file


In [11]:
# Write the questions to a file
name = "percentiles"
questions = []
for param in parameters:
    b = question_text(param)
    questions.append(b)
file = open(name + ".xml","w",encoding="utf8")
moodle_xml(name,questions,cloze_question,category = 'precentile/', iostream = file)
file.close()
print("Questions were saved in " + name + ".xml, that can be imported into Moodle")


Questions were saved in percentiles.xml, that can be imported into Moodle