Невозможно отобразить просмотр статистики для моего проекта PythonPython

Программы на Python
Ответить
Anonymous
 Невозможно отобразить просмотр статистики для моего проекта Python

Сообщение Anonymous »

У меня есть проект Python из онлайн-курса, в рамках которого мне поручено создать интерактивный инструмент обучения, который позволит пользователям создавать, практиковать и проверять свои знания, используя текстовые вопросы с несколькими вариантами ответов и произвольной формы. Программа будет отслеживать статистику пользователей и предоставлять возможности для управления вопросами.
На данный момент мне удалось написать несколько функций для добавления вопросов (либо вопросов с множественным выбором, либо вопросов произвольной формы), а затем сохранить их в файле CSV. Проблема в том, что после добавления и сохранения вопроса (или более) я до сих пор не могу их просмотреть (я не могу обнаружить никаких ошибок в моей функции view_statistics), так как я всегда получаю уведомление «Нет доступных вопросов». Пожалуйста помогите, буду очень признателен!
import csv
import time
import sys
import os
import platform
from colorama import init, Fore, Style
from questions import Question, Quiz, Freeform

questions = []

def loading():
'''
Short loading animation
'''
print("Loading...")
time.sleep(1)
if platform.system() == "Windows":
os.system('cls')
else:
os.system('clear')

def select_quiz_type():
'''
Selects the type of quiz to add questions to
'''
while True:
print("You can add quiz-type or freeform-type question(s)")
print("===============================================")
print("(a) Quiz")
print("(b) Freeform")
print("(c) Back to main menu")
print("===============================================")

question_type = input("Type in a or b (quiz/freeform): ").strip().lower()

if not question_type:
break
elif question_type == "a":
add_quiz_question()
elif question_type == "b":
add_freeform_question()
elif question_type == "c":
print("Loading...")
time.sleep(1)
if platform.system() == "Windows":
os.system('cls')
else:
os.system('clear')
main()
else:
print(Fore.RED + "Invalid value" + Style.RESET_ALL)

if len(questions) > 0:
save_questions(questions)
print(Fore.GREEN + "Questions saved successfully" + Style.RESET_ALL)
else:
print(Fore.RED + "No questions to save" + Style.RESET_ALL)

def add_quiz_question():
'''
Adds quiz questions and multiple possible answers
'''
question = input("Enter the quiz question: ").strip()
if not question:
print(Fore.RED + "Question cannot be empty." + Style.RESET_ALL)
return

choices = []

print("Enter multiple choices for the question: ")
while True:
choice = input("Choice: ").strip().lower()
if not choice:
print(Fore.RED + "No choices entered" + Style.RESET_ALL)
return
elif choice == "done":
if len(choices) < 3:
print(Fore.RED + "Minimum of 3 choices required" + Style.RESET_ALL)
choices = []
continue
break
else:
choices.append(choice)

loading()

try:
answer = int(input("Enter the number of the correct answer: ")) - 1
if answer < 0 or answer >= len(choices):
raise ValueError("Answer must be a valid choice number.")
except ValueError as e:
print(Fore.RED + f"Error: {e}" + Style.RESET_ALL)
return

questions.append(Quiz(question, choices, answer))

print(Fore.GREEN + "Your question has been added\n" + Style.RESET_ALL)

def add_freeform_question():
'''
Adds freeform questions and answers
'''
question = input("Enter the question: ")
answer = input("Enter the answer: ")

if not question or not answer:
print(Fore.RED + "Both question and answer cannot be empty")
return

questions.append(Freeform(question, answer))

print(Fore.GREEN + "Your question has been added\n" + Style.RESET_ALL)

def save_questions(questions, file_name="questions.csv"):
'''
Saves submitted questions to a CSV file
'''
with open(file_name, "w", newline='', encoding='utf-8') as file:
writer = csv.DictWriter(file, fieldnames=["question", "type", "choices", "answer", "enabled", "times_shown", "correct_count"])
writer.writeheader()
for question in questions:
row = question.__dict__.copy()
if isinstance(question, Quiz):
row['type'] = 'quiz'
row['choices'] = ', '.join(question.choices)
elif isinstance(question, Freeform):
row['type'] = 'freeform'
row['choices'] = ''
writer.writerow(row)

def load_questions(file_name="questions.csv"):
'''
Loads questions from a CSV file
'''
if not os.path.exists(file_name):
print("No questions found. The file does not exist.")
return []

questions = []
question_id = 1
with open(file_name, encoding='utf-8') as file:
reader = csv.DictReader(file)
for row in reader:
try:
enabled = row.get('enabled', 'True').lower() == 'true'
times_shown = int(row.get('times_shown', 0))
correct_count = int(row.get('correct_count', 0))
if row['type'] == 'quiz':
question = Quiz(row['question'], row['choices'].split(', '), row['answer'], enabled, times_shown, correct_count)
questions.append(question)
elif row['type'] == 'freeform':
question = Freeform(row['question'], row['answer'], row['enabled'])
question.id = question_id
questions.append(question)
question_id += 1
except ValueError as e:
print(Fore.RED + f"Error reading row: {row} - {e}" + Style.RESET_ALL)

return questions

def view_statistics(questions):
'''
Displays statistics for available questions
'''
if not questions:
print(Fore.RED + "No questions available" + Style.RESET_ALL)
return

print("Question Statistics:")
print("===============================================")
for question in questions:
correct_percentage = (question.correct_count / question.times_shown * 100 if question.times_shown > 0 else 0)
# active_status = (Fore.GREEN + "Enabled" + Style.RESET_ALL if question["active"] else Fore.RED + "Disabled" + Style.RESET_ALL)

print(f"ID: {question.id} | Status: {question.enabled}")
print(f"Type: {question.type}")
print(f"Question: {question.question}")
print(f"Times Shown: {question.times_shown}")
print(f"Correct Percentage: {question.correct_count}%")
print("===============================================")

def mode_validation(user_input):
'''
Validates user input for mode selection
'''
input_value = user_input.lower()

try:
if input_value not in ['a', 'b', 'c', 'd', 'e', 'f']:
raise ValueError("Please enter a valid mode")
except ValueError as e:
print(Fore.RED + f"Error: {e}")
return False

return True

def main():
'''
User can choose any of the available modes to run the program
'''
while True:
print(Fore.GREEN + "Choose a mode to run the program:" + Style.RESET_ALL)
print("=====================================")
print("(a) Adding questions")
print("(b) Statistics viewing")
print("(c) Disable / Enable questions")
print("(d) Practice mode")
print("(e) Test mode")
print("(f) End program")
print("=====================================")

selection = input("You choose: ")
if mode_validation(selection):
loading()
break

if selection == 'a':
select_quiz_type()
elif selection == 'b':
questions = load_questions()
view_statistics(questions)
elif selection == 'c':
...
elif selection == 'd':
...
elif selection == 'e':
...
else:
print("Exiting program...Bye!")
sys.exit(1)

if __name__ == "__main__":
main()

а вот мой файл questions.py для проведения занятий
class Question:
def __init__(self, question, answer, enabled=True, times_shown=0, correct_count=0):
self.question = question
self.answer = answer
self.enabled = enabled
self.times_shown = times_shown
self.correct_count = correct_count

def __str__(self):
return f"Question: {self.question}\nAnswer: {self.answer}\nEnabled: {self.enabled}"

class Quiz(Question):
def __init__(self, question, choices, answer, enabled=True):
super().__init__(question, answer, enabled)
self.choices = choices

def __str__(self):
return super().__str__() + f"\nChoices: {self.choices}"

class Freeform(Question):
def __init__(self, question, answer, enabled=True):
super().__init__(question, answer, enabled)


Подробнее здесь: https://stackoverflow.com/questions/793 ... on-project
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «Python»