Created
September 14, 2017 08:49
-
-
Save WorryingWonton/7220bb556739185311200fd0b114705c to your computer and use it in GitHub Desktop.
Answer to Steve's gist.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class QuestionA: | |
def __init__(self, question_dict): | |
self.question_dict = question_dict | |
class QuestionB: | |
def __init__(self, question_text, answer_dict): | |
self.question_text = question_text | |
self.answer_dict = answer_dict | |
qa = QuestionA({'What is a question?':'You just asked one.'}) | |
qb = QuestionB('What is a question?', {'You just asked one.': True}) | |
qa_text = [key for key in qa.question_dict.keys()] | |
# for key, value in qa_text.items(): | |
# print(key) | |
print(qa_text[0]) | |
qb_text = qb.question_text | |
print(qb_text) |
def __init__(self, question_dict):
self.question_dict = question_dict
class QuestionB:
def __init__(self, question_text, answer_dict):
self.question_text = question_text
self.answer_dict = answer_dict
qa = QuestionA({'What is a question?':'You just asked one.'})
qb = QuestionB('What is a question?', {'You just asked one.': True})
qa_text = [key for key in qa.question_dict.keys()]
qa_answers = list(qa.question_dict.values())[0]
# for key, value in qa_text.items():
# print(key)
print(qa_text[0])
print(qa_answers)
qb_text = qb.question_text
print(qb_text)```
class QuestionA:
def __init__(self, question_dict):
self.question_dict = question_dict
class QuestionB:
def __init__(self, question_text, answer_dict):
self.question_text = question_text
self.answer_dict = answer_dict
qa = QuestionA({'What is a question?':'You just asked one.'})
qb = QuestionB('What is a question?', {'You just asked one.': True})
qa_text = [key for key in qa.question_dict.keys()]
qa_answers = list(qa.question_dict.values())[0]
# for key, value in qa_text.items():
# print(key)
print(qa_text[0])
print(qa_answers)
qb_text = qb.question_text
print(qb_text)```
class Quiz:
def __init__(self, name, topic, difficulty, questions):
self.name = name
self.topic = topic
self.difficulty = difficulty
self.questions = questions
class Question:
def __init__(self, question_text, answer_dict):
self.question_text = question_text
self.answer_dict = answer_dict
def question_input(self):
return question_text
def answer_input(self):
return answer_dict
import json
class ObjectEncoder(json.JSONEncoder):
def default(self, obj):
return obj.__dict__
quiz = Quiz('My first quiz', 'basics', 1, [Question("What is the name of this quiz?", {"my first quiz": True})])
print(json.dumps(quiz, cls=ObjectEncoder))
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
qa
is an object (an instance of theQuestionA
class – an instance of a class is called an object)qa.question_dict
is adict
objectqa.question_dict.keys()
is adict_keys
object. It can be iterated over: https://docs.python.org/3.6/library/stdtypes.html#dict-views. It cannot be subscripted, i.e.qa.question_dict.keys()[0]
doesn't work.list
is a classlist
is subscriptable, solist(qa.question_dict.keys())[0]
will work, as long as there is at least one elementlist(...)
converts the expression...
(whatever it is) into an instance oflist
, if possibleThe last point could be implemented like so: