Setting Random Quiz Answers Instead of Set Choices

Discuss how to use the Ren'Py engine to create visual novels and story-based games. New releases are announced in this section.
Forum rules
This is the right place for Ren'Py help. Please ask one question per thread, use a descriptive subject like 'NotFound error in option.rpy' , and include all the relevant information - especially any relevant code and traceback messages. Use the code tag to format scripts.
Post Reply
Message
Author
User avatar
chris3spice
Regular
Posts: 44
Joined: Wed Nov 20, 2013 12:39 am
Projects: BKA24
Location: Kansas
Contact:

Setting Random Quiz Answers Instead of Set Choices

#1 Post by chris3spice » Wed Nov 20, 2013 4:54 pm

Hello, I started making my first real visual novel, I've made a couple shorter ones and some more a bit more complex, this one includes a quiz and that is where I am having issues.

What I am trying to accomplish:
I want to make a quiz where the three "wrong" answers are randomized and not set like they are in the list. I'm planning on making it to help learn Lithuanian so I'd want it pick wrong answers from a second list. I can create the second list like I did the first but I have no idea how to work it into the system. I know how to do this in C++ but this Python setup is different and I couldn't get it to work how I thought it would so I completely scratched that and need some pointers in the right direction.

This is the code I was using from Alex in another post:

Code: Select all

# Function that will shuffle answers
init python:

    def shuffle_answers(x):
        renpy.random.shuffle(x)
        return x
       
       
# The game starts here.
label start:
    # list of all possible questions
    # it consist of dictionaries, that describe each question:
    # key "question" has value of a question, key "category" makes sense for bot, key "answer" is a list of answers, that are lists [answer itself, right/wrong]
    # the order of this keys is not matter
    $ q_list = [
    {"question": "Taip", "answer": [ ["Yes", "right"], ["q", "wrong"], ["e", "wrong"], ["r", "wrong"] ]}, 
    {"question": "Labas Rytas", "answer": [ ["Hello", "right"], ["green", "wrong"], ["don\'t know", "wrong"], ["XoX", "wrong"] ]}, 
    {"question": "Labas Vakaras", "answer": [ ["Good Afternoon", "right"], ["5", "wrong"], ["100", "wrong"], ["42", "wrong"] ]},
    ]
   
    # game variables
    #####
   
    $ right_answers = 0     # result of quiz game
    $ quiz_length = 2       # number of questions in one game
    $ q_to_ask = []         # list of questions to ask in one game
   
    # let's choose some questions to play with
    while len(q_to_ask) < quiz_length:        # will work until we'll get enough questions for quiz
        $ a = renpy.random.choice (q_list)    # randomly pick a question from a list of all questions
        if a not in q_to_ask:                 # this question will be added only if we don't have it yet
            $ q_to_ask.append (a)

    label quize_game:                             # game loop
        $ a = renpy.random.choice (q_to_ask)      # randomly pick the question from a list
        $ q_to_ask.remove (a)                     # remove it from list to not to ask it twice

           
        $ b = shuffle_answers (a["answer"])        # let's shuffle answers
           
        # ugly part... next variables are necessary to fill menu
        $ question = a["question"]
        $ category = a["category"]
        $ answ_0 = b[0][0]
        $ answ_1 = b[1][0]
        $ answ_2 = b[2][0]
        $ answ_3 = b[3][0]
        menu:
            "Category - [category]\nQ - [question]"
            "[answ_0]":
                if b[0][1] == "right":        # checks the description of question if it's the right one
                    $ right_answers += 1
                    "That's true."
                else:
                    "Nope."
            "[answ_1]":
                if b[1][1] == "right":
                    $ right_answers += 1
                    "That's true."
                else:
                    "Nope."
            "[answ_2]":
                if b[2][1] == "right":
                    $ right_answers += 1
                    "That's true."
                else:
                    "Nope."
            "[answ_3]":
                if b[3][1] == "right":
                    $ right_answers += 1
                    "That's true."
                else:
                    "Nope."
       
        $ quiz_length -= 1
        if quiz_length > 0:
            jump quize_game
           
    "The result is - [right_answers]"

User avatar
pineapplepocky
Regular
Posts: 82
Joined: Wed Jul 31, 2013 10:10 pm
Location: California
Contact:

Re: Setting Random Quiz Answers Instead of Set Choices

#2 Post by pineapplepocky » Wed Nov 20, 2013 10:35 pm

oh wow, I'm in the same boat with you. I'll be watching this post for answers now...

Visual Novel Project: Destiny Limit --> http://www.destinylimit.net/
(facebook: http://www.facebook.com/destinylimitgame)

deviantart: http://pineapple-pocky.deviantart.com/ (and open for commissions)
blog: http://www.jackieloart.com/blog/

User avatar
chris3spice
Regular
Posts: 44
Joined: Wed Nov 20, 2013 12:39 am
Projects: BKA24
Location: Kansas
Contact:

Re: Setting Random Quiz Answers Instead of Set Choices

#3 Post by chris3spice » Tue Dec 03, 2013 2:05 am

I have partially figured it out! I created another list with the possible answers and then use random to insert them into the arrays, its messy but it works, the issue is I haven't figured out how to easily check for duplicates... to make sure the right answer isn't on there twice

Code: Select all

$ h_list = ["A", "E", "I", "O", "U"]
.....

       # ugly part... next variables are necessary to fill menu
        $ question = a["question"]

        if b[0][1] == "right":
            $ answ_0 = b[0][0]
        else:
            $ c = renpy.random.choice (h_list)
            $ answ_0 = c
        
        if b[1][1] == "right":
            $ answ_1 = b[1][0]
        else:
            $ c = renpy.random.choice (h_list)
            $ answ_1 = c
        
        if b[2][1] == "right":
            $ answ_2 = b[2][0]
        else:
            $ c = renpy.random.choice (h_list)
            $ answ_2 = c
        
        if b[3][1] == "right":
            $ answ_3 = b[3][0]
        else:
            $ c = renpy.random.choice (h_list)
            $ answ_3 = c

User avatar
Alex
Lemma-Class Veteran
Posts: 2981
Joined: Fri Dec 11, 2009 5:25 pm
Contact:

Re: Setting Random Quiz Answers Instead of Set Choices

#4 Post by Alex » Tue Dec 03, 2013 1:33 pm

Try to do it the similar way

Code: Select all

 # let's choose some questions to play with
    while len(q_to_ask) < quiz_length:        # will work until we'll get enough questions for quiz
        $ a = renpy.random.choice (q_list)    # randomly pick a question from a list of all questions
        if a not in q_to_ask:                 # this question will be added only if we don't have it yet
            $ q_to_ask.append (a)

Post Reply

Who is online

Users browsing this forum: Google [Bot]