-1

I'm a total noob here so I would really appreciate your help.

How to make a quiz game which would calculate different scores for different answers, regardless of their position (e.g. first or last on the displayed list)?

Currently, I'm doing this by slicing which means answers should be in a fixed order, but I don't want to have fixed answers order (e.g. if I shuffle the answers, I want it to recognize my correct_answer or fair_answer no matter it's position in the displayed answers list.

Inside quiz_brain.py inside QuizBrain class, I'm doing this within check_answer method:

    def check_answer(self, user_answer):
        """Check the answer from the user and assign it the appropriate score"""
        points_awarded = 0

        if user_answer == self.current_question.choices[0]:
            points_awarded = 4
        elif user_answer == self.current_question.choices[1]:
            points_awarded = 3
        elif user_answer == self.current_question.choices[2]:
            points_awarded = 2
        elif user_answer == self.current_question.choices[3]:
            points_awarded = 1

        self.score += points_awarded
        return points_awarded

and my question structure looks like this:

question_data = [
    {
        "question": "Kada ugledaš djevojku, kako ćeš joj prići??",
        "choices": {
            "correct_choice": "Cititat ćeš joj omiljene stihove Zoke Bosanca.",
            "good_choice": "Mala, dobro izgledaš. Ajmo na sok.",
            "decent_choice": "Čija si ti, mala?",
            "fair_choice": "Želiš li možda da zajedno popijemo neko pićence?",
        }
    },
    {
        "question": "Gdje ćeš izvesti djevojku na prvi spoj?",
        "choices": {
            "correct_choice": "U kvartovsku ćevabdžinicu, jest ću ćevape s lukom, kako i dolikuje.",
            "good_choice": "U ZOO, da posjeti svoju familiju.",
            "decent_choice": "Prošetat ćemo Maksimirom, možda se nešto i dogodi.",
            "fair_choice": "Izvest ću ju na kavu.",
        }
    },
    # Add more questions here
]

Git with my current code: https://github.com/KooMar22/KrkanQuiz

KooMar
  • 1
  • 4

2 Answers2

0
choiceType = self.current_question.choices.keys([self.current_question.choices.values().index(user_answer)])

    if choiceType == "correct_choice":
        points_awarded = 4

    elif choiceType == "good_choice":
        points_awarded = 3

    elif choiceType == "decent_choice":
        points_awarded = 2

    else: #fair_choice
        points_awarded = 1

Hi! This would help you. You can check this. If you need more clarification you can ask.

  • Thank you very much. I've tried this and it returns me the AttributeError: 'list' object has no attribute 'keys'" – KooMar Aug 06 '23 at 13:25
  • Is it really that no one here has any help or wants to contribute to a newbie? I have tried several methods and nothing seems to work. The questions have been re-defined and keys are now "answer_score" which have number assigned. I am not certain whether the error is within the quiz_brain responsible for getting the scores or within some UI thing. My attempt is here: https://github.com/KooMar22/KrkanQuiz – KooMar Aug 07 '23 at 18:42
0

Solved! Re-imagined the question_data and accessed it like this:

'''

    # Define type of answers
    correct_answer = self.current_question.correct_answer
    good_answer = self.current_question.good_answer
    decent_answer = self.current_question.decent_answer
    fair_answer = self.current_question.fair_answer

'''

For detailed solutions, feel free to take a look at: https://github.com/KooMar22/KrkanQuiz

KooMar
  • 1
  • 4