Here is another post about making random exercises with Python. I have many of this on the site, but this is very simple to make and I suggest you to give it a look, because it could help you to save a lot of time making stuff like this. I tryed this approach: I make a template with a multiline string, male a for loop of the line, give the random values to the values that you want to be different for every student (choosen among a range of possibilities, aka randrange) and then add some html to save the result in an easy to print way through the browser. That’s all. In case you want to see the code, you will find it at the end of the 2 parts of the live coding video.
Live Video Coding part 1: making random exercises with Python
Live Video Coding part 2: making random exercises with Python
Create the template
First, we are going to make a template, i.e. a simple multiline string with our ‘work’.
#%% from random import randrange testo = """Classwork n. 01 A new company has this data about his competitors: Hotel A, quality 3, price 4 Hotel B, quality 2, price 2 Hotel C, quality -1, price -2 The clients are comprised in the following groups: Families, quality 1, price 1 singles, quality 2, price 2 business, quality 4, price 4 Create the graph with the competitors and the segments and then position your new hotel. --------------------------- """
Then we are going to substitute the data we want to be randomized for every student, collecting them into a string (called html).
html = "" for student in range(14): html += "<h3>Homework n. " + str(student) + "</h3><br>" for line in text.splitlines(): q = randrange(-5, 5) p = q + randrange(-1, 1) line = line.replace("quality 3", "quality " + str(q)) line = line.replace("price 4", "price " + str(p)) html += line + "<br>" html += "<div style='page-break-before: always'>"
At the end we save an html file and we open it
with open("test1.html", "w", encoding="utf-8") as file: file.write(html) os.startfile("test1.html")
The repository with this code
Go here to go to the repository.
Full code
#%% from random import randrange import os text = """ From our data competitors quality and prices are: Hotel A, quality 3, price 4 Hotel B, quality 3, price 4 Hotel C, quality 3, price 4 Hotel D, quality 3, price 4 Our targets are: Families, quality 3, price 4 Business, quality 3, price 4 young, quality 3, price 4 After you positioned the competitors and the targets, position your new hotel in this market. """ html = "" for student in range(14): html += "<h3>Homework n. " + str(student) + "</h3><br>" for line in text.splitlines(): q = randrange(-5, 5) p = q + randrange(-1, 1) line = line.replace("quality 3", "quality " + str(q)) line = line.replace("price 4", "price " + str(p)) html += line + "<br>" html += "<div style='page-break-before: always'>" with open("test1.html", "w", encoding="utf-8") as file: file.write(html) os.startfile("test1.html")
Utilities