From d9dee5c376d523a8ebff61c5ce88739134c632f9 Mon Sep 17 00:00:00 2001 From: Shivam Sharma Date: Sun, 4 Jun 2023 17:51:09 +0530 Subject: [PATCH] Update Quiz Game.py Improve the time complexity in few lines of code --- Quiz Game/Quiz Game.py | 93 ++++++++++++++++++------------------------ 1 file changed, 39 insertions(+), 54 deletions(-) diff --git a/Quiz Game/Quiz Game.py b/Quiz Game/Quiz Game.py index 18ff9f8..f389c11 100644 --- a/Quiz Game/Quiz Game.py +++ b/Quiz Game/Quiz Game.py @@ -1,56 +1,41 @@ from questions import quiz - -def check_ans(question, ans, attempts, score): - """ - Takes the arguments, and confirms if the answer provided by user is correct. - Converts all answers to lower case to make sure the quiz is not case sensitive. - """ - if quiz[question]['answer'].lower() == ans.lower(): - print(f"Correct Answer! \nYour score is {score + 1}!") - return True - else: - print(f"Wrong Answer :( \nYou have {attempts - 1} left! \nTry again...") - return False - - -def print_dictionary(): - for question_id, ques_answer in quiz.items(): - for key in ques_answer: - print(key + ':', ques_answer[key]) - - -def intro_message(): - """ - Introduces user to the quiz and rules, and takes an input from customer to start the quiz. - Returns true regardless of any key pressed. - """ - print("Welcome to this fun food quiz! \nAre you ready to test your knowledge about food?") - print("There are a total of 20 questions, you can skip a question anytime by typing 'skip'") - input("Press any key to start the fun ;) ") - return True - - -# python project.py -intro = intro_message() -while True: - score = 0 - for question in quiz: - attempts = 3 - while attempts > 0: - print(quiz[question]['question']) - answer = input("Enter Answer (To move to the next question, type 'skip') : ") - if answer == "skip": - break - check = check_ans(question, answer, attempts, score) - if check: - score += 1 - break - attempts -= 1 - - break - -print(f"Your final score is {score}!\n\n") -print("Want to know the correct answers? Please see them below! ;)\n") -print_dictionary() -print("Thanks for playing! 💜") +score = 0 +skipped_questions = set() + +print("Welcome to this fun food quiz!") +print("Are you ready to test your knowledge about food?") +print("There are a total of 20 questions. You can skip a question anytime by typing 'skip'.") +input("Press any key to start the fun ;) ") + +for question in quiz: + attempts = 3 + while attempts > 0: + print(quiz[question]['question']) + answer = input("Enter Answer (To move to the next question, type 'skip'): ") + + if answer == "skip": + skipped_questions.add(question) + break + + if quiz[question]['answer'].lower() == answer.lower(): + score += 1 + break + + attempts -= 1 + print(f"Wrong Answer :( \nYou have {attempts} attempt(s) left! \nTry again...") + +print(f"\nYour final score is {score}!\n") + +print("Skipped questions:") +for question_id in skipped_questions: + print(quiz[question_id]['question']) +print("\n") + +print("Correct answers:") +for question_id, ques_answer in quiz.items(): + print(f"Question: {ques_answer['question']}") + print(f"Answer: {ques_answer['answer']}") + print() + +print("\nThanks for playing! 💜")