import pandas as pd
from operator import attrgetter
class Question(object):
def __init__(self, Question, task, reference, aAnswer, bAnswer, cAnswer, dAnswer, letterAnswer, Relevance):
self.Question = Question
self.task = task
self.reference = reference
self.aAnswer = aAnswer
self.bAnswer = bAnswer
self.cAnswer = cAnswer
self.dAnswer = dAnswer
self.letterAnswer = letterAnswer
self.Relevance = Relevance
df = pd.read_excel('Test Bank 2021.xlsx')
questions = df.values.tolist()
question_instances = []
for p in questions:
question_instances.append(Question(*p))
I've started the above project where I'm creating a testing program. The object attributes are listed and self-explanatory. The "Relevance" attribute references the different tests that are in the database of questions.
For example, if I wanted to issue someone a "Math" test, I would only want to pull from the objects with the "Math" value for the "Relevance" attribute.
I've tried googling this, but the only things that come up are how to print the attributes. But what I want is to print the other attributes of the objects based on the value of the user input.
It would start something like this:
testName = Input("What test would you like to give? ")
testQuestions = Input("How many questions? ")
After this, I would want to filter the large (1000 item) "question_instances" list and select a random sample of questions based on the amount of questions selected, only taking from the questions in the correct relevance selected.
CodePudding user response:
Is this what you are looking for?
import random
import pandas as pd
class Question(object):
def __init__(self, Question, task, reference, aAnswer, bAnswer, cAnswer, dAnswer, letterAnswer, Relevance):
self.Question = Question
self.task = task
self.reference = reference
self.aAnswer = aAnswer
self.bAnswer = bAnswer
self.cAnswer = cAnswer
self.dAnswer = dAnswer
self.letterAnswer = letterAnswer
self.Relevance = Relevance
df = pd.read_excel('Test Bank 2021.xlsx')
questions = df.values.tolist()
question_instances = [Question(*p) for p in questions]
testName = input("What test would you like to give? ")
testQuestions = int(input("How many questions? "))
filtered_questions = [q for q in question_instances if q.Relevance == testName]
selected_questions = random.sample(filtered_questions, testQuestions)
for question in selected_questions:
print(question.Question)
print(question.aAnswer)
print(question.bAnswer)
print(question.cAnswer)
print(question.dAnswer)