Skip to content

Commit 88d887a

Browse files
committed
add write csv
1 parent 51a6399 commit 88d887a

File tree

1 file changed

+21
-7
lines changed

1 file changed

+21
-7
lines changed

game05_1/quiz.py

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,16 +73,29 @@ def prossima_domanda():
7373
def init_file_risposte():
7474
"""Crea il file risposte con header se non esiste."""
7575
if not os.path.exists(NOME_FILE_RISPOSTE):
76-
with open(NOME_FILE_RISPOSTE, mode='w', newline='', encoding='utf-8') as f:
77-
writer = csv.writer(f)
78-
writer.writerow(["nome_utente", "id_domanda", "numero_risposta_fornita", "tempo_risposta"])
76+
df = pl.DataFrame({
77+
"nome_utente": pl.Series([], dtype=pl.Utf8),
78+
"id_domanda": pl.Series([], dtype=pl.Int64),
79+
"numero_risposta_fornita": pl.Series([], dtype=pl.Int64),
80+
"tempo_risposta": pl.Series([], dtype=pl.Int64),
81+
})
82+
df.write_csv(NOME_FILE_RISPOSTE)
7983

8084

8185
def salva_risposta(nome, id_dom, numero_risposta, tempo_risposta):
8286
init_file_risposte()
83-
with open(NOME_FILE_RISPOSTE, mode='a', newline='', encoding='utf-8') as f:
84-
writer = csv.writer(f)
85-
writer.writerow([nome, id_dom, numero_risposta, tempo_risposta])
87+
88+
nuova_riga = pl.DataFrame({
89+
"nome_utente": [str(nome)],
90+
"id_domanda": [int(id_dom)],
91+
"numero_risposta_fornita": [int(numero_risposta)],
92+
"tempo_risposta": [int(tempo_risposta)],
93+
})
94+
95+
# Append leggendo + concatenando (approccio semplice e robusto)
96+
df_esistente = pl.read_csv(NOME_FILE_RISPOSTE)
97+
df_finale = pl.concat([df_esistente, nuova_riga])
98+
df_finale.write_csv(NOME_FILE_RISPOSTE)([nome, id_dom, numero_risposta, tempo_risposta])
8699

87100

88101
# ───────────────── DISEGNO ─────────────────
@@ -200,6 +213,7 @@ def on_key_down(key):
200213
nome_utente += ch
201214
return
202215

216+
203217
def tick():
204218
global secondi_mancanti
205219
if not game_over and not entering_name:
@@ -218,7 +232,7 @@ def tick():
218232
prossima_domanda()
219233

220234

221-
# ───────────────── CONTROLLO START ─────────────────
235+
# ───────────────── CONTROLLO START / RESTART ─────────────────
222236

223237
def start_game():
224238
global entering_name, nome_utente

0 commit comments

Comments
 (0)