Skip to main content

Another Test Maker Program




I have changed a little bit the code for making tests. This time I want to make a test where you got to guess the region that a certain province (italian) belongs to... so

I had this data, in this form

Piemonte,Torino
Piemonte,Vercelli
Piemonte,Novara
Piemonte,Cuneo
Piemonte,Asti
Piemonte,Alessandria
Piemonte,Biella
Piemonte,Verbano-Cusio-Ossola
Valle d'Aosta,Aosta
Lombardia,Varese
Lombardia,Como
Lombardia,Sondrio
Lombardia,Milano

Lombardia,Bergamo
....

etcetera

I took the lines from the regioniprovince.txt


from random import shuffle

with open("regioneprovincia.txt") as file:
 dati = file.readlines()

# I erase the \n at the end of the lines
for c in dati:
 dati[dati.index(c)] = c.replace("\n","")

# I create a list of list for each line ['piemonte', 'torino'] instead of 'piemonte,torino'
x = 0
for a in dati:
 dati[x] = dati[x].split(',')
 x += 1

# I create a dictionary with name or region and empty values

province_dict = {}
province = []

for a,b in dati:
 province_dict[b] = ''
 province += [b] # here I got all the province

# I populate the regions keys with the province values
for a,b in dati:
 province_dict[b] += a + ","


shuffle(province)

# Crea la lista con le soluzioni (qual è la regione di Avellino: Campania)
# quindi province[0] ha la soluzione in regioni[0]

regione = []
for r in province:
 regione += [province_dict[r]]
# THEN I MADE THE 30 QUESTIONS
for x in range(30):
 print("Dove si trova",province[x]+"?")
 scelta = list(set(regione.copy())) #create a list identical to regioni
 scelta.pop(scelta.index(regione[x])) # delete the right one from the choices
 shuffle(scelta) # mix the alternative wrong answerw
 # Now I take 3 random answere
 a = scelta.pop()
 b = scelta.pop()
 c = scelta.pop()
 risposte = [a,b,c,regione[x]]
 shuffle(risposte) # I shuffle them
 for items in risposte:
  print("- in",items)
 print()
 print("R:",regione[x])
 print()

# here is the output (not all the 30 question...)
---------------------------------------------------------------------
Dove si trova Crotone?
- in Sardegna,
- in Piemonte,
- in Calabria,
- in Veneto,

R: Calabria,

Dove si trova Chieti?
- in Toscana,
- in Piemonte,
- in Abruzzo,
- in Veneto,

R: Abruzzo,

Dove si trova Livorno?
- in Toscana,
- in Lazio,
- in Molise,
- in Puglia,

R: Toscana,

Dove si trova Pistoia?
- in Abruzzo,
- in Toscana,
- in Molise,
- in Veneto,

R: Toscana,

Dove si trova Bologna?
- in Puglia,
- in Basilicata,
- in Emilia-Romagna,
- in Valle d'Aosta,

R: Emilia-Romagna,

Dove si trova Rovigo?
- in Sardegna,
- in Lombardia,
- in Veneto,
- in Abruzzo,

R: Veneto,

Dove si trova Parma?
- in Emilia-Romagna,
- in Veneto,
- in Abruzzo,
- in Molise,

R: Emilia-Romagna,

Dove si trova Nuoro?
- in Toscana,
- in Piemonte,
- in Molise,
- in Sardegna,

R: Sardegna,

Dove si trova Vibo Valentia?
- in Umbria,
- in Veneto,
- in Calabria,
- in Campania,

R: Calabria,

Dove si trova Vercelli?
- in Piemonte,
- nelle Marche,
- in Basilicata,
- in Friuli-Venezia Giulia,

R: Piemonte,

Dove si trova Firenze?
- in Calabria,
- in Campania,
- in Toscana,
- in Abruzzo,

R: Toscana,

Dove si trova Lucca?
- in Toscana,
- in Campania,
- in Lombardia,
- in Friuli-Venezia Giulia,

R: Toscana,

Dove si trova Caltanissetta?
- in Toscana,
- in Sicilia,
- in Trentino-Alto Adige,
- in Valle d'Aosta,

R: Sicilia,

Dove si trova Catanzaro?
- in Puglia,
- in Calabria,
- in Abruzzo,
- in Sicilia,


Comments

Popular posts from this blog

Widgets for Jupyter Notebook: a text input widget

Widgets for Jupyter notebook ¶ Let's import the module ipywidgets into the Jupyter Notebook from ipywidgets import widgets from ipywidgets import * from traitlets import * Now we import the display function from IPython ¶ let's attach a function to the event on_submit After we run this cell, we can go up and write something in the text widget and after you submit the text you wrote will be printed after the cell from IPython.display import display text = widgets . Text () display ( text ) def handle_submit ( sender ): print ( "Thank you for entering this text:" , text . value ) text . on_submit ( handle_submit ) Thank you for entering this text: Ciao

Image in Jupyter and PIL step by step

Hi, """ Hi, we will see a step by step tutorial about PIL and IPython.core.display modules to create images from other images and diplaying them in Jupyter notebook """ # What we will do # Create a card # 1. Take a pic of a heart # 2. Create an image blanck the size of a card 90*130 # 3. Paste the heart in the middle # 4. show the card """ As first step wi will simply display an image on the notebook. I will show two way to display the image with 'display' from IPhyton a. Using the open method of PIL.Image (named Img) b. Using the Image method from the IPython.core.display module """ # 1. Take the pic of a heart from IPython.core.display import Image , display from PIL import Image as Img heart = 'img/heart.png' display ( Image ( heart )) display ( Img . open ( heart )) # 2. Create an image blanck the size of a card 90*130 # 3. Paste the heart in the middle #...

Tkinter tutorial 1

Ok, this time we are going to talk about the tkinter module, a module to make GUIs (graphic user interface) for Python. Until now we have just used the console or the jupyter nootebook (and its widgets) to show the output. Now we are going to use the usual windows for linux, windows or MacOs. 1. IMPORT THE MODULE To use the tkinter modules (on python 3) you have to import them, for example, like this: from tkinter import * In this way you don't have to write tkinter. and then the classes you are going to use, but just the name of the classes. It is "faster", but it can sometime coflict with other names of other modules you can import. For now, we will use this way to import tkinter, anyway. 2. CREATE A WINDOW root = Tk () root . geometry ( "400x300+500+300" ) root . mainloop () About the geometry method of root (istance of Tk()): We passed a string argument to the method: 400x300 is the size of the window 500+300 are the position o...