Skip to content

Instantly share code, notes, and snippets.

@WorryingWonton
Created September 14, 2017 08:49
Show Gist options
  • Save WorryingWonton/7220bb556739185311200fd0b114705c to your computer and use it in GitHub Desktop.
Save WorryingWonton/7220bb556739185311200fd0b114705c to your computer and use it in GitHub Desktop.
Answer to Steve's gist.
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)
@WorryingWonton
Copy link
Author

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)```


@WorryingWonton
Copy link
Author

WorryingWonton commented Sep 14, 2017

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
    

@m0xb
Copy link

m0xb commented Sep 14, 2017

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