[error] pop from empty list

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
Mjhay
Regular
Posts: 61
Joined: Sat Jan 16, 2021 2:29 pm
Location: Philippines
Contact:

[error] pop from empty list

#1 Post by Mjhay »

my quiz game is all working . the timer of the game is 20 seconds every question . if you run out of time the it will jump to the next question and if its done, screen quiz result will display and the remarks is "Unaswered".
and its working, EXCEPT when you run out of time on the LAST ITEM of quiz (5/5 items) . it goes to error.

the error is:

Code: Select all

I'm sorry, but an uncaught exception occurred.

While running game code:
  File "game/script.rpy", line 907, in script call
    call quiz_game(q_listMC_1, 5) # calling a quiz, passing name of questions list and quiz length
  File "game/script.rpy", line 584, in script
    $ a = q_to_ask.pop()           # pick the question (it will be removed from the list)
  File "game/script.rpy", line 584, in <module>
    $ a = q_to_ask.pop()           # pick the question (it will be removed from the list)
IndexError: pop from empty list

-- Full Traceback ------------------------------------------------------------

Full traceback:
  File "game/script.rpy", line 907, in script call
    call quiz_game(q_listMC_1, 5) # calling a quiz, passing name of questions list and quiz length
  File "game/script.rpy", line 584, in script
    $ a = q_to_ask.pop()           # pick the question (it will be removed from the list)
  File "C:\Users\isle\Documents\THESIS\renpy\renpy-7.3.5-sdk\renpy\ast.py", line 914, in execute
    renpy.python.py_exec_bytecode(self.code.bytecode, self.hide, store=self.store)
  File "C:\Users\isle\Documents\THESIS\renpy\renpy-7.3.5-sdk\renpy\python.py", line 2028, in py_exec_bytecode
    exec bytecode in globals, locals
  File "game/script.rpy", line 584, in <module>
    $ a = q_to_ask.pop()           # pick the question (it will be removed from the list)
IndexError: pop from empty list

Windows-8-6.2.9200
Ren'Py 7.3.5.606
CSSTIME 1.0
Mon Jun 07 09:33:52 2021
how can i fix that? im stuck in this error :( any one can understand the situation? and thanks for the help ..

here is the timer of the quiz:

Code: Select all

screen countdown(duration=20):

    default time_remaining = duration

    timer 1.0 repeat True action SetScreenVariable("time_remaining", max(time_remaining - 1.0, 0))
    bar value AnimatedValue(time_remaining, duration) xsize 200 align (0.5, 0.15)

    if time_remaining > 20:
        text str(time_remaining) align(0.5, 0.08) color "#fff" size 50
    elif time_remaining > 15:
        text str(time_remaining) align(0.5, 0.08) color "#07a81e" size 50
    elif time_remaining > 10:
        text str(time_remaining) align(0.5, 0.08) color "#faf104" size 50
    elif time_remaining > 5:
        text str(time_remaining) align(0.5, 0.08) color "#eb6e08" size 50

    elif time_remaining > 0:
      text str(time_remaining) align(0.5, 0.08) color "#f00" size 50
    else:
      text str(time_remaining) align(0.5, 0.08) color "#f00" size 50

    if time_remaining <= 0:
      $ q_result.append([a["question"], "-", "Unanswered"])                ### if you runout of time it will display in result
      timer 0.01 action [Jump('quize_game'), Hide("countdown")]
here is the quiz code:

Code: Select all

default q_listMC_lo1 = [
    {"question": "It is an electronic device that manipulates information, or data. It has the ability to store, retrieve, and process data.?", "answer": [ ["Computer", "right"], ["cpu", "wrong"], ["Modem", "wrong"], ["Internet", "wrong"] ], "category": "Multiple Choice"},
    {"question": "is the primary component of a computer that processes instructions. It runs the operating system and applications, constantly receiving input from the user or active software programs. It processes the data and produces output, which may stored by an application or displayed on the screen.?", "answer": [ ["cpu", "right"], ["computer", "wrong"], ["motherboard", "wrong"], ["internet", "wrong"] ], "category": "multiple choice"},
      ]
      .
      .
      

default q_listToF_lo1 = [
    {"question": "After adding and removing any other system components, make sure that you unplug your power supply. ", "answer": [ ["False", "right"], ["True", "wrong"] ], "category": "True or False"},
    {"question": "After starting the installation, read carefully the documentation and procedures on any hardware and software settings that may be required", "answer": [ ["False", "right"], ["True", "wrong"] ], "category": "True or False"},
       ]
	.
	.
	
label quiz_game(questions_list, q_length):

    # game variables
    $ q_list = questions_list
    $ q_result = []                    # the question that is asked of the quiz store in here
    $ right_answers = 0                # result of quiz game
    $ wrong_answers = 0                # result of wrong answer
    $ quiz_length = q_length           # number of questions in one game
    $ unlocked = [right_answers]
    $ q_asked=0
    $ passingScore = 3

    # let's choose some questions to play with
    $ q_to_ask = renpy.random.sample(q_list, quiz_length) # list of questions to ask in one game

    label quiz_game_loop:              # game loop
        $ q_asked=0
        $ quiz_length = q_length
        $ a = q_to_ask.pop()           # pick the question (it will be removed from the list)
        python: # let's shuffle answers
            b = []
            for q in shuffle_answers (a["answer"]):
                b.append((q[0], q))    # (answer to show, [answer, 'right/wrong'])

        show screen countdown # timer of the quiz
        
        # the question
        $ narrator("Category -  {}\nQuestion -  {}".format(a["category"], a["question"]), interact=False)

        # the answers - result will be the chosen answer
        #$ renpy.block_rollback()
        $ res = renpy.display_menu(b)

        $ result = res[1] # ("right"/"wrong")
        $ answer = res[0] # answer (text)
        # evaluate the result
        if result == "right":
            $ renpy.block_rollback()
            $ right_answers += 1
        else:
            $ renpy.block_rollback()
            $ wrong_answers += 1

        # store the question and chosen answer in q_result
        $ q_result.append([a["question"], answer, result])     

        $ q_asked += 1


        if q_to_ask: # if we still got questions to ask
            jump quiz_game_loop

        return # return to game
        
label start:
	.
	.
	"Quiz 1{p}Category - Multiple Choice"
  	call quiz_game(q_listMC_1, 5) # calling a quiz, passing name of questions list and quiz length
  	.
  	.
  	"Quiz 2{p}Category - True or False"
  	call quiz_game(q_listMC_1, 5) # calling a quiz, passing name of questions list and quiz length



User avatar
Imperf3kt
Lemma-Class Veteran
Posts: 3792
Joined: Mon Dec 14, 2015 5:05 am
itch: Imperf3kt
Location: Your monitor
Contact:

Re: [error] pop from empty list

#2 Post by Imperf3kt »

I'm not at home right now, so cannot look closely, but I have a blackjack game that had a similar issue about a year ago.

I'll take a closer look at your code once I'm home in several hours, but you may find use in my old posts on the subject and the helpful answer I received, here
viewtopic.php?f=8&t=57616

I don't know how much use it will be
Warning: May contain trace amounts of gratuitous plot.
pro·gram·mer (noun) An organism capable of converting caffeine into code.

Current project: GGD Mentor

Twitter

User avatar
Ocelot
Lemma-Class Veteran
Posts: 2401
Joined: Tue Aug 23, 2016 10:35 am
Github: MiiNiPaa
Discord: MiiNiPaa#4384
Contact:

Re: [error] pop from empty list

#3 Post by Ocelot »

Trace what happens when you ask the last question:
1) Question is picked and removed from q_to_ask list. q_to_ask is now empty.
2) Question is asked. If you answer it, it will check if there are questions left in q_to_ask and stop the game if there are none.
3) If you fail to answer it in time, you will unconditially jump to the beginning of quiz loop and try to pick next question. From empty list.

Suggestion: make jump quiz_game_loop unconditional. Instead move check if not q_to_ask: to be first thing you do when label starts and return if it is true.
< < insert Rick Cook quote here > >

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

Re: [error] pop from empty list

#4 Post by Alex »

Mjhay wrote: Sun Jun 06, 2021 10:03 pm ...
When player seeing the menu with the answers, the game waits for his/her interaction. Then, the game evaluates the values, that was returned by player's choice. So, you can make your countdown screen return some values that represent the unanswered question.

Try it like

Code: Select all

screen countdown(t=3.0):
    bar value AnimatedValue(0, t, t, t) xsize 300 align(0.95, 0.05)
    timer t action [Return(["---", "unanswered"]), Hide("countdown")]
And in quiz_game_loop label

Code: Select all

        # the answers - result will be the chosen answer
        $ res = renpy.display_menu(b)

        $ result = res[1] # ("right"/"wrong")
        $ answer = res[0] # answer (text)
        # evaluate the result
        if result == "unanswered":
            $ wrong_answers += 1
            "Too slow..."
            
            
        elif result == "right":
            $ right_answers += 1
            "That's true !"

        else:
            $ wrong_answers += 1
            "Nope!"

User avatar
Mjhay
Regular
Posts: 61
Joined: Sat Jan 16, 2021 2:29 pm
Location: Philippines
Contact:

Re: [error] pop from empty list

#5 Post by Mjhay »

Thanks for all of your suggestions emperf3kt, ocelot and to you alex :)
Im busy this days before because yesterday is my wedding .
I will try all your suggestions when im back at home . Thanks again :)

Post Reply

Who is online

Users browsing this forum: Google [Bot]