Create a True False test with random order of sentences with Python

Did you ever wanted to create a true false test where the senteces are randomized. This simple code do this in this way:

# code to make a true false test

from random import shuffle

list_of_sentences = """I flussi turistici internazionale sono in crescita costante tranne per una flessione nel 2009
L’Europa cresce ad un ritmo superiore all’Asia per quanto riguarda i flussi turistici
In Italia si registrano ogni anno più di 100 milioni di arrivi
Il turismo business in Italia supera il 40%
In Italia i turisti sono attratti dal patrimonio artistico e culturale, dalla gastronomia, dal clima e dal paesaggio
L’Italia perde posizioni nelle classifiche mondiali
Il turismo genera intorno al 10% del PIL
La Bilancia turistica è in passivo
In Italia il punto di forza del turismo è rappresentato dalle reti di trasporto
La rivalutazione dell’euro rende conveniente per gli stranieri visitare l’Italia""".splitlines()


def writefile(num_compiti):
    """Write txt file with the sentences in random order (uncomment in function numerodicompiti)"""
    with open(f"vf{num_compiti}.txt", 'w', encoding="utf-8") as file:
        for n, line in enumerate(list_of_sentences):
            file.write(str(n + 1) + "\t[v][f]\t" + line + "\n")
        print(f"The file vf{num_compiti}.txt has been written")


def vf(num_compiti=1):
    """Create a number of Classwork, if x==1 then it will print just one cw"""
    for x in range(num_compiti):
        shuffle(list_of_sentences)
        for n, line in enumerate(list_of_sentences):
            print(str(n + 1) + "\t[v][f]\t" + list_of_sentences[n])
        # uncomemnt this if you do not want to create a txt file with classwork
        writefile(num_compiti)


print("Domande Vero Falso")
# NUMERO DI COMPITI
vf(num_compiti=1)

The result on the console is this (similar to the text in the txt file created)

Domande Vero Falso
1	[v][f]	Il turismo genera intorno al 10% del PIL
2	[v][f]	In Italia si registrano ogni anno più di 100 milioni di arrivi
3	[v][f]	La rivalutazione dell’euro rende conveniente per gli stranieri visitare l’Italia
4	[v][f]	L’Europa cresce ad un ritmo superiore all’Asia per quanto riguarda i flussi turistici
5	[v][f]	Il turismo business in Italia supera il 40%
6	[v][f]	L’Italia perde posizioni nelle classifiche mondiali
7	[v][f]	In Italia i turisti sono attratti dal patrimonio artistico e culturale, dalla gastronomia, dal clima e dal paesaggio
8	[v][f]	La Bilancia turistica è in passivo
9	[v][f]	I flussi turistici internazionale sono in crescita costante tranne per una flessione nel 2009
10	[v][f]	In Italia il punto di forza del turismo è rappresentato dalle reti di trasporto
The file vf1.txt has been written

Utilities

Published by pythonprogramming

Started with basic on the spectrum, loved javascript in the 90ies and python in the 2000, now I am back with python, still making some javascript stuff when needed.