fragen und Antworten aus CSV

This commit is contained in:
Taeufelmaennchen committed 2023-12-15 23:30:50 +01:00
1 parent f8d0dc43c5
commit c101a51593
7 files changed
+461 -5

No files matched your search

+41
View File
@@ -0,0 +1,41 @@
import csv # CSV handling
import random
import os
#file = "C:/Users/bruno/Documents/__CODING__/0-git (Group)/robotik-wise23/robo/fragen1.csv"
#file = os.getcwd() + "fragen1.csv"
#file = os.path.join(os.getcwd(), "robo", "fragen1.csv") # Windows Entwicklung
file = os.path.join(os.getcwd(), "fragen1.csv") # Raspi
#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
# for i in q_a: # For every element in "q_a"
# print("[" + str(i["index"]) + "] Frage: " + i["question"] + "\n[" + str(i["index"]) + "] Antwort: " + i["answer"] + "\n") # Print "question", "answer" and "index" for current element
while True:
# Zufällig eine Frage auswählen
zufallige_frage = random.choice(q_a)
ID = zufallige_frage["index"]
frage = zufallige_frage["question"]
antwort = zufallige_frage["answer"]
#print(f"wir sindin {os.getcwd()}")
# Frage und Antwort ausgeben
print(f"Frage:\t({ID})\t{frage}")
print(f"Antwort:\t{antwort}")
# Auf Eingabe warten
input("Drücke Enter für die nächste Frage...")