def __init__(self, question_bank): self.window = Tk() self.window.title("Quiz Game") self.window.config(padx=20, pady=20, bg=THEME_COLOR) self.score = 0 self.score_lable = Label(text=f'Score: {self.score}') self.score_lable.grid(row=0, column=1) self.quiz = QuizBrain(question_bank) self.canvas = Canvas(width=300, height=250, bg='white') text = self.quiz.next_question() self.quiz_text = self.canvas.create_text(150, 125, width=280, text=text, fill=THEME_COLOR, font=FONT) self.canvas.grid(row=1, column=0, columnspan=2, pady=50) false_img = PhotoImage(file="images/false.png") self.false_button = Button(image=false_img, highlightthickness=0, command=self.choose_false_button) self.false_button.grid(row=2, column=0) true_img = PhotoImage(file="images/true.png") self.true_button = Button(image=true_img, highlightthickness=0, command=self.choose_true_button) self.true_button.grid(row=2, column=1) self.window.mainloop()
def main(): print(logo) question_bank = [] num_of_questions = len(question_data) for i in range(num_of_questions): question_bank.append( Question(question_data[i]["text"], question_data[i]["answer"])) Quizzer = QuizBrain(question_bank) questions = random.sample(question_bank, Quizzer.no_of_questions_to_ask) print(f"I found {num_of_questions} questions.") print( f"I have chosen {Quizzer.no_of_questions_to_ask} random questions\n" \ f"to ask you. Good luck :)\n" ) while questions: if Quizzer.question(questions.pop()): Quizzer.score += 1 print(f'Correct, you earn a point [{Quizzer.score}/{Quizzer.no_of_questions_to_ask}]\n') else: print('That is incorrect\n') print(f"final score: {Quizzer.score}/{Quizzer.no_of_questions_to_ask}")
def main(): response = requests.get("https://opentdb.com/api.php?amount=50&type=boolean") openTriviaDB = json.loads(response.text) print("\n", ascii_art, "\nWelcome to The Quiz!") num_questions = int(input("How many questions do you want? (0-50) ")) question_bank = [Question(openTriviaDB["results"][i]["question"], openTriviaDB["results"][i]["correct_answer"]) for i in range(num_questions)] quiz = QuizBrain(question_bank) print() while quiz.still_has_questions(): quiz.next_question() print(f"You've completed the quiz!\n Your final score was {quiz.score}/{len(question_bank)}") option = input("Do you want to play again? y/n ") if option == 'y': main() else: exit()
class QuizInterface: def __init__(self, question_bank): self.window = Tk() self.window.title("Quiz Game") self.window.config(padx=20, pady=20, bg=THEME_COLOR) self.score = 0 self.score_lable = Label(text=f'Score: {self.score}') self.score_lable.grid(row=0, column=1) self.quiz = QuizBrain(question_bank) self.canvas = Canvas(width=300, height=250, bg='white') text = self.quiz.next_question() self.quiz_text = self.canvas.create_text(150, 125, width=280, text=text, fill=THEME_COLOR, font=FONT) self.canvas.grid(row=1, column=0, columnspan=2, pady=50) false_img = PhotoImage(file="images/false.png") self.false_button = Button(image=false_img, highlightthickness=0, command=self.choose_false_button) self.false_button.grid(row=2, column=0) true_img = PhotoImage(file="images/true.png") self.true_button = Button(image=true_img, highlightthickness=0, command=self.choose_true_button) self.true_button.grid(row=2, column=1) self.window.mainloop() def check_answer(self, answer): if self.quiz.check_answer(answer): self.score += 1 self.score_lable.config(text=f'Score: {self.score}') if self.quiz.still_has_questions(): text = self.quiz.next_question() self.canvas.itemconfig(self.quiz_text, text=text) else: messagebox.showinfo(title="Game's Over", message=f"Your's core is: {self.score}") self.window.destroy() def choose_true_button(self): self.check_answer('true') def choose_false_button(self): self.check_answer('false')
def main(): question_bank = [] for elem in question_data: question_bank.append(Question(elem["question"], elem["correct_answer"])) quiz = QuizBrain(question_bank) while quiz.still_has_questions(): quiz.next_question() print("You've completed the quiz") print(f"Your final score was: {quiz.score}/{quiz.question_number}.")
def main(): question_bank = [] for qa in question_data: question_bank.append(Question(qa['question'], qa['correct_answer'])) shuffle(question_bank) qb = QuizBrain(question_bank) while qb.still_has_question() and qb.alive: qb.next_question() if qb.alive: print( f"Congratulations, you have completed the quiz with a score of {qb.score}" )
def main(): """ Creates instances of the classes to implement the quiz :return: nothing """ question_bank = [] for question in question_data: new_quest = Question(question['question'], question['correct_answer']) question_bank.append(new_quest) quiz = QuizBrain(question_bank) while quiz.still_has_questions(): quiz.next_question() print('You have completed the quiz') print(f'The final score is: {quiz.score}/{len(quiz.question_list)}')
def reset_quiz(self): self.quiz = QuizBrain(data.construct_questions()) self.buttons_state(ACTIVE) self.reset_button.config(state=DISABLED) self.get_next_question()
def main(): question_bank = [] for question in question_data: question_text = question["question"] question_answer = question["correct_answer"] new_question = Question(question_text, question_answer) question_bank.append(new_question) quiz = QuizBrain(question_bank) quiz_ui = QuizInterface(quiz) print("You've completed the quiz") print(f"Your final score was: {quiz.score}/{quiz.question_number}")
def quiz(): question_bank = [] for item in question_data: new_question = Question(item['text'], item['answer']) question_bank.append(new_question) quiz_brain = QuizBrain(question_bank) while quiz_brain.still_has_questions(): quiz_brain.next_question() quiz_brain.end_game_message()
from data import question_data from question_model import Question from quiz_brain import QuizBrain #Initializing the questions question_bank = [] for i in question_data: question_bank.append(Question(i["text"], i["answer"])) brainage = QuizBrain(question_bank) more_questions = True #Main loop of the game while more_questions == True: brainage.next_question() more_questions = brainage.still_has_questions() print( f"\nThat is the end of the quiz. Your final score is {brainage.score} out of {brainage.question_number}" )
from data import question_data from question_model import Question from quiz_brain import QuizBrain question_bank = [] for question in question_data: question_text = question["question"] question_answer = question["correct_answer"] question_category = question["category"] new_question = Question(question_text, question_answer, question_category) question_bank.append(new_question) quiz = QuizBrain(question_bank) while quiz.still_have_question(): quiz.check_answer() print(f"You've completed the quiz") print(f"Your final score was: {quiz.score}/{quiz.question_number}")
from question_model import Question from data import question_data from quiz_brain import QuizBrain question_bank = [] for entry in range(len(question_data)): question = Question(question_data[entry]["text"], question_data[entry]["answer"]) question_bank.append(question) Quiz = QuizBrain(question_bank) while Quiz.still_has_questions(): Quiz.next_question() print("\n\n\You have completed the quiz") print(f"Your final score is: {Quiz.score}/{Quiz.question_number} ")
""" Day 17. Creating classes Sample: Quiz game """ from question_model import Question from data import question_data from quiz_brain import QuizBrain question_bank = [] for question in question_data: question_text = question["text"] question_answer = question["answer"] new_question = Question(question_text, question_answer) question_bank.append(new_question) quiz = QuizBrain(question_bank) while quiz.still_has_question(): quiz.next_question() print("You've completed the quiz! 🎉") print(f"Your final score is: {quiz.score}/{quiz.question_number}")
from flask import Flask, render_template from data import question_data from question import Question from quiz_brain import QuizBrain app = Flask(__name__) question_bank = [] for question in question_data: question_bank.append(Question(question['question'], question['correct_answer'])) quiz_brain = QuizBrain(question_bank) @app.route('/') def home(): return render_template('index.html', question=quiz_brain.next_question(), score=quiz_brain.score) @app.route('/<user_ans>') def check_answer(user_ans): quiz_brain.check_answer(user_ans) if quiz_brain.still_has_question(): return render_template('index.html', question=quiz_brain.next_question(), score=quiz_brain.score) else: quiz_brain.reset() return render_template('quiz_over.html', score=quiz_brain.score)
from question_model import Question from quiz_brain import QuizBrain from data import question_data questions_bank = [] for question in question_data: questions_bank.append(Question(question["text"], question["answer"])) quiz = QuizBrain(questions_bank) while not quiz.all_questions_asked(): quiz.next_question() quiz.show_final_score()
from question_model import Question from data import question_data from quiz_brain import QuizBrain question_bank = [] for q in question_data: question = Question(q["question"], q["correct_answer"]) question_bank.append(question) quiz = QuizBrain(question_bank) while quiz.still_has_questions(quiz.question_number, len(quiz.question_list)): quiz.next_question()
from question_model import Question from data import question_data from quiz_brain import QuizBrain question_bank = [] for question in question_data: new_question = Question(question["question"], question["correct_answer"]) #question_text = question["text"] #question_answer = question["answer"] #print(new_question.text) #new_question = Question(question_text, question_answer) question_bank.append(new_question) #print(question_bank[0].answer) quiz = QuizBrain(question_bank) while quiz.still_has_questions( ) == True: #if quiz still has questions remaining quiz.next_question() print(f"You've completed the quiz!") print(f"your final score was:{quiz.score}/{quiz.question_number}")
from question_model import Question from data import question_data from quiz_brain import QuizBrain question_bank = [] for question in question_data: question_text = question["text"] question_answer = question["answer"] new_question = Question(question_text, question_answer) question_bank.append(new_question) quiz = QuizBrain(question_bank) while quiz.still_have_question(): quiz.next_question() if not quiz.still_have_question(): print( f"You have finished the quiz.\n Your final score is {quiz.score}/{quiz.question_number}." )
from data import question_data from question_model import Question from quiz_brain import QuizBrain question_bank = [] for item in question_data: question_bank.append(Question(item['question'], item['correct_answer'])) quiz_control = QuizBrain(question_bank) while quiz_control.still_has_question(): quiz_control.next_question() print( f"You've completed the quiz!\nYour final score is {quiz_control.score}/{quiz_control.question_number}" )
from question_model import Question from data import GenerateData from quiz_brain import QuizBrain from ui import QuizInterface # def ques_gen(): # question_data = GenerateData().question_data # return question_data # # question_bank = [] # for question in ques_gen(): # question_text = question["question"] # question_answer = question["correct_answer"] # new_question = Question(question_text, question_answer) # question_bank.append(new_question) quiz = QuizBrain() quiz_ui = QuizInterface(quiz) # while quiz.still_has_questions(): # quiz.next_question() print("You've copleted the quiz") print(f"Your final score was: {quiz.score}/{quiz.question_number}")
def main(): question_data = get_questions(NUM_QUESTIONS) quiz = QuizBrain(question_data) quiz.ui = QuizInterface(quiz)
from question_model import Question from data import question_data from quiz_brain import QuizBrain question_bank = [] for i in question_data: question_bank.append(Question(i.get('question'), i.get('correct_answer'))) brain = QuizBrain(question_bank) while brain.still_has_questions(): brain.next_question() print("Game Over!") print(f"Your Final Score: {brain.score}/{len(question_bank)}")
from question_model import Question from data import question_data, question_data_2, question_data_api from quiz_brain import QuizBrain from ui import QuizInterface questionBank = [] # for item in question_data: # questionBank.append(Question(item["text"],item["answer"])) # for item in question_data_2: # questionBank.append(Question(item["question"],item["correct_answer"])) # Get questions through API for item in question_data_api: questionBank.append(Question(item["question"], item["correct_answer"])) quiz = QuizBrain(questionBank) quizUI = QuizInterface(quiz) # while quiz.still_has_question(): # quiz.nextQuestion() print("You've completed the quiz") print(f"Your final score was: {quiz.score}/{quiz.questionNumber}")
from question_model import Question from data import question_data from quiz_brain import QuizBrain question_bank = [] for question in question_data: new_question = Question(question["text"], question["answer"]) question_bank.append(new_question) quiz_brain = QuizBrain(question_bank) while quiz_brain.still_has_questions(): quiz_brain.next_question()
from question_model import Question from data import question_data from quiz_brain import QuizBrain questionBank = [] for question in question_data: questionText = question["text"] questionAnswer = question["answer"] newQuestion = Question(questionText, questionAnswer) questionBank.append(newQuestion) quiz = QuizBrain(questionBank) while quiz.stillHasQuestions(): quiz.nextQuestion() print("You've completed the quiz, congrats!!!") print(f"Your final score was: {quiz.score}/{quiz.questionNumber}")
from question_model import Question from data import question_data from quiz_brain import QuizBrain question_bank = [] for question in question_data: question_text = question["text"] question_answer = question["answer"] new_question = Question(question_text, question_answer) question_bank.append(new_question) quiz = QuizBrain(question_bank) quiz.next_question()
from question_model import Question, question_bank from data import question_data from quiz_brain import QuizBrain # Initialise new QuizBrain object quiz = QuizBrain(question_bank) quiz_over = False while not quiz_over: quiz_over = quiz.next_question() if not quiz_over: pass else: quiz.print_score()
from question_model import Question from data import question_data from quiz_brain import QuizBrain from ui import QuizInterface question_bank = [] for question in question_data: question_text = question["question"] question_answer = question["correct_answer"] new_question = Question(question_text, question_answer) question_bank.append(new_question) quiz = QuizBrain(question_bank) quiz_ui = QuizInterface(quiz)
from question_model import Question from data import question_data from quiz_brain import QuizBrain Question_Bank = [] for question in question_data: Q_text = question["text"] Q_answer = question["answer"] new_question = Question(Q_text, Q_answer) Question_Bank.append(new_question) quiz = QuizBrain(Question_Bank) while quiz.still_has_questions(): quiz.next_question() if quiz.score == len(Question_Bank): print("Congratulations!,You got all questions right") else: print("You have completed the quiz") print(f"Your final score is {quiz.score}/{len(Question_Bank)}")