Page 1 of 1

How can I make a quiz?

Posted: Mon Dec 22, 2014 8:08 pm
by rukiruki
Hello everyone. I've just signed up to the website to finally ask a question.

I'm thinking of making a video game character quiz where you get to choose from a number of questions and the game comes up with a result for you based on your decisions. I wonder if people do this with Ren'py?
https://www.playbuzz.com/jonb10/which-norse-god-are-you

I need three things: the characters, the questions and a results table.

I have the characters.

Code: Select all

init:
    $ mage = 0
    $ hunter = 0
    $ guardian = 0
    $ assassin = 0
    $ warrior = 0
I have the questions.

Code: Select all

    menu:
        "Mage":
            $ mage += 1
        "Hunter":
            $ hunter += 1
        "Guardian":
            $ guardian += 1
        "Assassin":
            $ assassin += 1
        "Warrior":
            $ warrior += 1
What I can't seem to figure out is how to create a results table where the character with the highest number gets chosen. If I set lots of them at a time, it only selects the one closest to the top and disregards the rest of the characters. For example, if I had the most points in hunter but my mage has more points than guardians, it will jump to mage. How do I counter this?

Code: Select all

    if mage > hunter:
        jump mage
        
    if mage > guardian:
        jump mage
        
    if mage > assassin:
        jump mage
        
    if mage > warrior:
        jump mage
        
    if hunter > mage:
        jump hunter
My coding experience is extremely insufficient. If I had to try to write it without knowing what code it needed, it would probably look something like this:

Code: Select all

    if mage > hunter,guardian,assassin,warrior:
        jump mage
This doesn't seem to work though. I thank you in advance.

Re: How can I make a quiz?

Posted: Mon Dec 22, 2014 9:14 pm
by philat

Re: How can I make a quiz?

Posted: Wed Dec 24, 2014 12:42 am
by Breci
In case someone need a solution I made a quick one.

So I used what is the easiest to expand in my personal opinion.

Why I am doing it like this?
I don't like having long "if" statement to check everything.
I personally like to have a code that can be changed or improved over time so I adapted the data structure for it. With it it's possible to add new scores during the game or even remove some without having to change the algorithm to find the maximum.

Data structure:
You can easily add more fields in the dictionary in your code or in the initialisation

Code: Select all

define classes = {'mage': 0,'hunter': 0,'guardian': 0,'assassin': 0, 'warrior': 0}
python functions:
Simply find the highest value and return the class name, not the value itself.

Code: Select all

init python:

    def highestScore(classes):
        max = 0
        res = ""
        for char in classes:
            if max < classes[char]:
                res = char
                max = classes[char]
        return res
How to change the value of a score :
Increase by 1:

Code: Select all

$ classes['mage'] += 1
Change value:

Code: Select all

$ classes['mage'] = 10
Getting and testing the result:

Code: Select all

    $ res = highestScore(classes)
    if res == 'mage':
        jump mage
    if res == 'hunter':
        jump hunter
    if res == 'guardian':
        jump guardian
    if res == 'assassin':
        jump assassin
    if res == 'warrior':
        jump warrior
    "[res] : empty" #To test if no max is found 
If you want to display a value:

Code: Select all

    $ nb = classes['mage']
    "mage : [nb]"

Re: How can I make a quiz?

Posted: Wed Dec 24, 2014 7:16 pm
by philat
Breci wrote:

Code: Select all

init python:

    def highestScore(classes):
        max = 0
        res = ""
        for char in classes:
            if max < classes[char]:
                res = char
                max = classes[char]
        return res
Have you thought about how to deal with ties? AFAIK, dictionaries are unordered, so if there were ties (i.e. both mage and hunter scored 3, for example) this would return the first key/value pair evaluated at random. Maybe you're fine with that, but it seems like something that should be pointed out.

Re: How can I make a quiz?

Posted: Wed Dec 24, 2014 8:45 pm
by Breci
The only reference he provided is a basic online quizz. I don't think they are taking care about it.
Also, I'm based on his own code where he don't say that he try to handle it and don't have any line about it.

So yeah I assumed he does not care about it.