Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Weather-Music-Chatbot/quiz.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
110 lines (85 sloc)
4.33 KB
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
# ++++++++++++++++++++++++++++++++++++++++++++ CODE WRITTEN WITH HELP OF THE GUIDE LINKED BELOW, FOLLOWED BEGINNING STEPS UNTIL MINUTE 20 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ | |
# https://youtu.be/r2ZN0mTDnPc | |
# Only tutorial for python I could find related to this API | |
import json # json module allows us to make use of our API | |
import pandas # used to decode our json file | |
import random # useful for making our quiz questions random, as well as other factors | |
from urllib.request import urlopen # used for opening and reading our URL | |
# API does not require a key, it is an open public API! | |
with urlopen( | |
"https://opentdb.com/api.php?amount=50&category=12&type=multiple") as api_page: # utilizing our URL as "api_page" | |
api_data = json.loads(api_page.read().decode()) | |
api_data_frame = pandas.DataFrame(api_data["results"]) | |
# print(api_data_frame.columns) | |
def preload_api_data( | |
idx): # with this function, we will be able to set our questions and answers as well as sorting out our data | |
# effectively | |
questions = api_data_frame["question"][idx] | |
correct_ans = api_data_frame["correct_answer"][idx] | |
wrong_ans = api_data_frame["incorrect_answers"][idx] | |
formatting = [ | |
# with this formatting list, we will make it so weirdly displayed characters are replaced by the proper ones. This happens because of html entities.More info @ https://www.w3.org/wiki/Common_HTML_entities_used_for_typography | |
("#039;", "'"), | |
("&'", "'"), | |
(""", '"'), | |
("<", "<"), # had to adapt to display errors showed, as the ones on the guide were different and not enough | |
(">", ">"), # there might be other errors underlying in the questions? | |
("&", "&"), | |
("&ldquo:", '"'), | |
("&rdquo:", '"'), | |
("“", '"'), | |
("Ü", "Ü"), | |
("ü", "ü"), | |
("µ", "µ"), | |
("é", "é") | |
] | |
for tuple in formatting: | |
questions = questions.replace(tuple[0], tuple[1]) | |
correct_ans = correct_ans.replace(tuple[0], tuple[ | |
1]) # Tuples used for formatting the html entities to the correct characters | |
for tuple in formatting: | |
wrong_ans = [char.replace(tuple[0], tuple[1]) for char in wrong_ans] | |
parameters["question"].append(questions) | |
parameters["correct"].append(correct_ans) | |
all_ans = wrong_ans + [correct_ans] | |
random.shuffle(all_ans) | |
parameters["answer1"].append(all_ans[0]) | |
parameters["answer2"].append(all_ans[1]) # placeholder non-optimized code | |
parameters["answer3"].append(all_ans[2]) | |
parameters["answer4"].append(all_ans[3]) | |
parameters = { | |
"question": [], | |
"answer1": [], | |
"answer2": [], | |
"answer3": [], # actual parameters for each question, answer, etc. | |
"answer4": [], | |
"correct": [], | |
"score": [0], | |
"index": [random.randint(0, 14)] | |
} | |
preload_api_data(parameters["index"][-1]) | |
# ++++++++++++++++++++++++++++++++++++++++++++ ORIGINAL CODE +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ | |
# Getting the quiz to actually run had to be done without the help of the tutorial, as the tutorial has a different application. | |
def quiz(n, score): # By taking an n value (which represents the question number) we can make a loop that makes it so only 15 questions are printed, once at a time. | |
while n < 15: # I had thoughts of using recursion instead, but found the while loop to be best suited. | |
n = n + 1 | |
y = str(n) + ". " | |
print(y + parameters["question"][-1]) | |
print(parameters["answer1"][-1]) | |
print(parameters["answer2"][-1]) | |
print(parameters["answer3"][-1]) | |
print(parameters["answer4"][-1]) | |
p_answer = input() | |
if p_answer == parameters["correct"][-1]: | |
print("Correct!") | |
score = score + 1 | |
parameters["index"].pop() # pop not removing repeated question from index? | |
parameters["index"].append(random.randint(0, 14)) | |
preload_api_data(parameters["index"][-1]) | |
# Repeated questions sometimes appear | |
else: | |
print("Incorrect!") | |
parameters["index"].pop() | |
parameters["index"].append(random.randint(0, 14)) | |
preload_api_data(parameters["index"][-1]) | |
print("The quiz is over!\nYou got a total score of " + str(score) + " points!") |