Number of questions Django/Python

Number of questions:

Try to create a new game of 20 questions of medium difficulty (MEDIUM) in the Art category.

It was acting weird, right?

This is definitely not a desirable situation. It follows that Open Trivia DB does not have enough questions from a given category with a given level of difficulty, so it returns us an error. And we do not handle it wisely:

    try:
        question = quiz.get_question()
        quiz.save(request)
        return render(request, 'game.html', vars(question))
    except IndexError as x:
        return redirect('/finish')

because we assume that the end of the questions = the end of the game. What makes sense if the API has the right number of questions ...

The response code from the Open Trivia website in this case is 1. Looking at the documentation (https://opentdb.com/api_category.php), we will find out what each code means: enter image description here

Hint

We have to think about how to solve this problem. It would be nice if the answer, apart from the error code, would include all possible tasks for a given category and level of difficulty - but unfortunately it is not. We have to adapt to the existing API, changing anything there is beyond our reach.

We have three possible solutions:

  • or we will provide the user with as many questions as we are able to (and not as many as asked)
  • we inform the user that he does not have as many questions as he is requesting and we will ask him to choose a different value
  • or we will complete the questions with those of the "adjacent" difficulty level. For example, if we have 10 questions on the HARD difficulty level, and the user wanted to get 15 HARD questions - we will add 5 more questions from the MEDIUM category to the 10 questions mentioned.

The first step is common to each of the approaches - we need to discover how many questions we can get out of Open Trivia DB. Fortunately, we don't have to discover it by trial and error. The documentation contains information about an additional endpoint: enter image description here

For the art category (Art, id = 25), the answer from https://opentdb.com/api_count.php?category=25 is as follows:

{
   "category_id":25,
   "category_question_count":{
      "total_question_count":26,
      "total_easy_question_count":8,
      "total_medium_question_count":10,
      "total_hard_question_count":8
   }
}

the first and second options are simple - it is enough that until the moment we build the list of categories, we will pull the above-mentioned data even more and in the first case replace the number of questions selected by the user with the available one, and in the second, print a nice error. The third option is more difficult because it requires us to decide how many questions from which difficulty level we should download. For example, if we have 10 questions on the hard difficulty level, and the user wanted 15 hard questions - we will add 5 more questions from the medium category to the mentioned 10 questions.

While it is easy to say what level of difficulty is "adjacent" for hard or easy (it will be medium), in the opposite case - for the neighboring medium it will be easy or hard? It seems fairest to add "half" questions from both difficulty levels. We also need to protect ourselves against a situation such as in the Art category - where if we want to play a quiz consisting of 20 difficult questions, we have to provide 8 difficult, 10 medium and 2 easy questions from our application so that there are 20 of them in total.

If you decide on the most difficult solution, you will have to write a function that will "select" questions from other difficulty levels. I suggest that you check at the very beginning that the desired number of questions is not greater than the maximum number of questions for all levels. When you get it over with, this function should look like a while loop, which will spin as many times as there are no questions, and select one question in each of its cycles for the pool prepared for the user.

Another idea is to build, on the example of the Art category - where if we want to play a quiz consisting of 20 difficult questions, we have to provide 8 difficult, 6 medium and 6 easy questions from our application, so that there are 20 of them in total, code like:

lvls = ['easy', 'medium', 'hard']

requested_questions = request.POST['quantity']

requested_difficulty = request.POST['difficulty']

question_deficiency = requested_questions - total_hard_question_count

question_deficiency_per_lvl = question_deficiency / 2

questions_for_user = get_questions_for_lvl(requested_difficulty, requested_questions)

lvls.remove(requested_difficulty)

for level in lvls
   questions_for_user.extend(get_questions_for_lvl(level, question_deficiency_per_lvl))

But then there may be a situation where in the remaining difficulty levels there will not be an equal number of questions, e.g. by asking 20 questions from a medium:

  • in easy will be 2
  • in hard it will be 10
  • there will be 10 in the medium

according to this code we will be able to take 10 medium, 5 hard and 5 easy, but .... in easy there are only 2 questions.

However, I suggest a loop :) possibly a creative approach to this solution :)

Code: https://github.com/Mar3eczek17/ProjektPlusPython/tree/master/pythonplussources/sdaquiz

Back to Top