import os from flask import Flask, redirect, render_template, request, session import csv # CSV handling import random app = Flask(__name__) def load_database(): file = os.path.join(os.path.expanduser("~"), "Austausch", "fragen1.csv") #with open(file, newline="", encoding="utf-8") as csvfile: # with open(file, newline="", encoding="cp1252") as csvfile: # Code wird als Excel Windows-1252 Input CSV gelesen database = csv.reader(csvfile, delimiter=";") # Validating the "csvfile" into "database" with the delimiter "," (Comma Seperated Values) q_a = [] # Empty list for all questions and answers for row in database: # alle Zeilen in der Datenbank durchlaufen index = int(row[0]) # Laufende Nummer question = str(row[1]) # Frage (ist der erste Teil) answer = str(row[2]) # Antwort (3. Spalte in der CSV Datenbank) q_a.append({"index": index,"question": question, "answer": answer}) # Eine Liste bauen return q_a fragen_liste = load_database() random.shuffle(fragen_liste) current_index = 0 correct_sum = 0 last_correct = None max_correct_sum = 5 debugtxt="" @app.route('/') def quiz(): return render_template('quiz.html', frage=fragen_liste[current_index], last_correct=last_correct,debugtxt=debugtxt, correct_sum=correct_sum, max_correct_sum = max_correct_sum) @app.route('/submit_quiz', methods=['POST']) def submit_quiz(): global current_index global fragen_liste global debugtxt global correct_sum # Überprüfen Sie die Antwort und aktualisieren Sie current_index user_answer = request.form.get('answer') correct_answer = fragen_liste[current_index]['answer'] if user_answer.lower() == correct_answer.lower(): last_correct = True correct_sum += 1 debugtxt = correct_answer + " ist richtig! passt zu deiner Angabe: " + user_answer elif user_answer == "42": correct_sum += 1 debugtxt = "Korrekt! Die Antwort auf alle Fragen ist 42." else: last_correct = False # debugtxt += "\n" # debugtxt += "\n" debugtxt = "falsch: Richtig wäre " +correct_answer + " / und nicht: " + user_answer correct_sum = 0 current_index += 1 # Überprüfen, ob wir alle Fragen beantwortet haben if current_index >= len(fragen_liste): fragen_liste = load_database() random.shuffle(fragen_liste) current_index=0 if correct_sum >= 5: fragen_liste = load_database() random.shuffle(fragen_liste) current_index=0 debugtxt += "\nGewonnen! - Gut gemacht!" print ("gewonnen! - Fahre 10 Sek!") correct_sum =0 # Leiten Sie den Benutzer zur nächsten Frage weiter return redirect('/')