With the following script you can take a text with multiple choices like these
What is ..... the first the second the fourth the fifth What is .... x y z v
Where the first answer is the right one.
The script will:
- add the number to each question
- add a letter to each answer
- shuffle the answers
- create a file with the test (in html)
- create a file with the correct answer (in txt)
The code
from tkinter import filedialog import os from random import shuffle ''' THIS CODE TAKES A TXT FILE WITH QUESTIONS WITH 4 ANSWER DIVIDED BY AN EMPTY LINE, GIVE A NUMBER TO QUESTION SHUFFLES ANSWERS AND GIVES THEM A LETTER IT CREATES A CORRETTORE whith the right answersin the correttore.txt ''' a = filedialog.askopenfilename(initialdir = ".",title = "Select file",filetypes = (("text","*.txt"),("all files","*.*"))) # a = "preliminari_2.txt" with open(a, "r", encoding="utf-8") as file: file = file.readlines() html = "" newdom = 1 risposte = [] letters = "abcd" countdom = 1 corrette = [] for line in file: if line == "\n": newdom = 1 continue if newdom == 1: html += f"<b>{countdom}. {line}</b><br>" newdom = 0 countdom += 1 else: risposte.append(line) if len(risposte) == 4: corrette.append(f"{countdom - 1}: {risposte[0]}") shuffle(risposte) for n, risposta in enumerate(risposte): html += f"{letters[n]}. {risposta}<br>" risposte = [] newdom = 1 html += "<br>" with open("output.html", "w", encoding="utf-8") as file: file.write(html) with open("correttore.txt", "w") as file: file.write("".join(corrette)) os.startfile("output.html")
Subscribe to the newsletter for updates
Tkinter templates
My youtube channel
Twitter: @pythonprogrammi - python_pygame