Skywalkersc
Skywalkersc

Reputation: 11

Trying to build a multiple choice quiz but score keeps reseting

I'm trying to build a multiple choice quiz that randomly selects questions from a pool and then removes already asked questions before randomly selecting another while also keeping score (in this case 'i' represents the score). My current problem is that the score keeps getting reset because (I believe) the "frage()" call at the end of the else/if statement sets i to 0

I tried defining score outside of the frage() method and generally moving it around. I also tried making a chain of elif statements so I wouldn't have to use frage() at the end of every input prompt, but I feel that made the code unnecessarily bulky and ugly.

Did I start off in a completely wrong direction for this quiz, or am I just making a rookie mistake somewhere?

#chooses a question randomly from the pool and prints it

def frage():

    random_idx = randint(0, len(question_pool) - 1)
    print(question_pool[random_idx] ['frage'])
    print(" ")
    print("A:", question_pool[random_idx] ['a'],)
    print("B:", question_pool[random_idx] ['b'],)
    print("C:", question_pool[random_idx] ['c'],)
    print("D:", question_pool[random_idx] ['d'])

#input prompt
    score = 0
    for i in range(0, len(question_pool) - 1):
        guess = input("Was glaubst?: ")
        guess = guess.lower()

#Result of input
        if guess == question_pool[random_idx]['antwort']:
            print(" ")
            print("Ja freilich")
            score = (score + 1)
            print((str(score)) + (" Punkte"))
            del question_pool[random_idx]
            frage()

        else:
            print(" ")
            print("Auweia")
            print((str(i)) + (" Punkte"))
            del question_pool[random_idx]
            frage()

Upvotes: 0

Views: 73

Answers (3)

Sohaib Jundi
Sohaib Jundi

Reputation: 1664

You you using i which is the loop counter for printing the score. Use another counter for the points
Edit:
Remove the recursive calls to frage too, and it should work fine

Upvotes: 1

Shizzen83
Shizzen83

Reputation: 3529

You should not use del when you have more proper ways to extract data, here is an example:

import random

def frage(pool):
    pool = pool.copy()
    poolLength = len(pool)
    score = 0
    while poolLength > 0:
        randomQuestion = pool.pop(random.randint(0, poolLength - 1))
        poolLength -= 1 #avoids recalculating length on each iteration. It can be impactful on big lists.
        for answerChoice in ['a', 'b', 'c', 'd']:
            print(answerChoice.upper() + ':', randomQuestion[answerChoice])
        if input("Was glaubst?: ").lower() == randomQuestion['antwort']:
            score += 1
            print("\nJa freilich\n" + str(score) + " Punkte")
        else:
            print("\nAuweia")
    return score

Upvotes: 0

Anubhav Dinkar
Anubhav Dinkar

Reputation: 413

score,as you know, is a variable that you re-intialize every time frage() is called. To solve this, you can return the value of score to a variable, outside of the function.

So instead of calling frage() from inside the function itself, return the score (return score) to a variable and then call frage() again.

So instead of :

if guess == question_pool[random_idx]['antwort']:
            print(" ")
            print("Ja freilich")
            score = (score + 1)
            print((str(score)) + (" Punkte"))
            del question_pool[random_idx]
            frage()

do:

if guess == question_pool[random_idx]['antwort']:
            print(" ")
            print("Ja freilich")
            score = (score + 1)
            print((str(score)) + (" Punkte"))
            del question_pool[random_idx]
            return score

When you call the function, do this:

quiz_score += frage()

This statement could be repeated in a while loop till a certain criteria is met (when your game ends)

Upvotes: 0

Related Questions