[英]How do I print class objects based on an attribute value obtained from user input?
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))
我已經開始了上面的項目,我正在創建一個測試程序。 列出了 object 屬性並且不言自明。 “相關性”屬性引用問題數據庫中的不同測試。
例如,如果我想對某人進行“數學”測試,我只想從“相關性”屬性具有“數學”值的對象中提取。
我試過用谷歌搜索這個,但唯一出現的是如何打印屬性。 但我想要的是根據用戶輸入的值打印對象的其他屬性。
它會像這樣開始:
testName = Input("What test would you like to give? ")
testQuestions = Input("How many questions? ")
在此之后,我想過濾大型(1000 項)“question_instances”列表和 select 基於所選問題數量的隨機問題樣本,僅從所選正確相關性的問題中提取。
這是你想要的?
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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.