Create a test in html from a txt file

Creating a test in html with python from a txt file

If you want to make a multiple choice easily, with random order of answers, you can find the code here:

import os
from random import shuffle

def tag (stuff, decorator):
	stuff = stuff.strip()
	decorator1 = "<" + decorator + ">"
	decorator2 = "</" + decorator + ">"
	return decorator1 + stuff + decorator2


def openfile(tag_domanda, tag_risposte):
	"Show mixed answers"
	try:
		# open one of the files in the dir
		stringa = [str(n) + " " + f for n,f in enumerate(os.listdir())]
		filenumber = input(f"Choose {stringa}")
		filechosen = os.listdir()[int(filenumber)]
		openfile = open(filechosen, 'r', encoding='utf-8')
		# show the file
		lines = openfile.readlines()

		# Check if it's the start, it it's the right answer
		inizio = True

		# answers list to shuffle them at the end
		risposte = []
		for l in lines:
			#             STAMPA LA DOMANDA
			if inizio:
				print(tag(l, tag_domanda))
				inizio = False

			# INSERISCE LE RISPOSTE IN UNA LISTA

			else:
				if l != "\n":
					risposte.append(l)


			# QUANDO TROVA LA LINEA VUOTA MISCHIA E STAMPA LE RISPOSTE

			if l == "\n":
				# RISPOSTA GIUSTA
				giusta = risposte[0]      #  RISPOSTA GIUSTA
				shuffle(risposte)
				if tag_risposte == 'li':
					print("<ol>")
				for r in risposte:
					print(tag(r, tag_risposte))
				if tag_risposte == 'li':
					print("</ol>")

			# LA RISPOSTA GIUSTA VIENE STAMPATA

				print("Risposta esatta: ", giusta, end="")
				inizio = True
				print("")
				risposte = []

		openfile.close()
	except IndexError:
		print("Numero del file troppo alto")
		openfile()
# the argument:
# b = bold
# h1 = header 1
openfile("b", "li")

The text file should be like this:

Qual è l'obiettivo principale del marketing oggi al mercato?
vendere
produrre di più
soddisfare i clienti
fidelizzare i clienti

Il turista fa parte del...
turismo attivo
turismo passivo
turismo produttivo
turismo residenziale

L'escursionista
non pernotta
pernotta

Il turista
pernotta
non pernotta

Il turismo domestico è composto dai turisti stranieri che visitano l'Italia
falso
vero

* add 2 empty lines at the end

The output will be like this:

Choose ['0 question_to_list.py', '1 turismo.txt']1
<b>Qual è l'obiettivo principale del marketing oggi al mercato?</b>
<ol>
<li>produrre di più</li>
<li>vendere</li>
<li>fidelizzare i clienti</li>
<li>soddisfare i clienti</li>
</ol>
Risposta esatta:  vendere

<b>Il turista fa parte del...</b>
<ol>
<li>turismo passivo</li>
<li>turismo produttivo</li>
<li>turismo residenziale</li>
<li>turismo attivo</li>
</ol>
Risposta esatta:  turismo attivo

<b>L'escursionista</b>
<ol>
<li>non pernotta</li>
<li>pernotta</li>
</ol>
Risposta esatta:  non pernotta

<b>Il turista</b>
<ol>
<li>non pernotta</li>
<li>pernotta</li>
</ol>
Risposta esatta:  pernotta

<b>Il turismo domestico è composto dai turisti stranieri che visitano l'Italia</b>
<ol>
<li>falso</li>
<li>vero</li>
</ol>
Risposta esatta:  falso

I wish you liked this code. Please, if you do, share this on twitter.

Utilities