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