"List index out of range" error, please help

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
Hoyoka
Newbie
Posts: 4
Joined: Mon Feb 22, 2016 6:46 am
Projects: VN Princess of the Lost kingdom
Location: Slovakia
Contact:

"List index out of range" error, please help

#1 Post by Hoyoka » Mon Feb 22, 2016 7:09 am

I am having a problem with my VN. I am getting an error I have never seen before and I tried almost everything to fix it. Maybe it is just a small mistake I did not notice, because I am just a beginner and I am making my first visual novel, so any help would be appreciated.

I'm sorry, but an uncaught exception occurred.

While running game code:
File "game/script.rpy", line 9944, in script
$ karta5 = renpy.random.choice(karty)
File "game/script.rpy", line 9944, in <module>
$ karta5 = renpy.random.choice(karty)
IndexError: list index out of range

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

Full traceback:
File "game/script.rpy", line 9944, in script
$ karta5 = renpy.random.choice(karty)
File "C:\Users\AdamVeronika\Downloads\Veronika\renpy-6.99.7-sdk.7z\renpy-6.99.7-sdk\renpy\ast.py", line 805, in execute
renpy.python.py_exec_bytecode(self.code.bytecode, self.hide, store=self.store)
File "C:\Users\AdamVeronika\Downloads\Veronika\renpy-6.99.7-sdk.7z\renpy-6.99.7-sdk\renpy\python.py", line 1461, in py_exec_bytecode
exec bytecode in globals, locals
File "game/script.rpy", line 9944, in <module>
$ karta5 = renpy.random.choice(karty)
File "/home/tom/ab/x64lucid-deps/install/lib/python2.7/random.py", line 275, in choice
IndexError: list index out of range


This is line 9944:

$ karta5 = renpy.random.choice(karty)

The block looks like this:
if cards == 4:
$ karta5 = renpy.random.choice(karty)
$ karty.remove(karta5)
show expression karta5 at truecenter with moveinleft
pause 1.0
hide expression karta5

And this is the list named "karty":
$ karty = ["k1", "k2", "k3", "k4", "k5", "k6", "k7", "k8", "k9",
"k10", "k11", "k12", "k13", "k14", "k15", "k16", "k17",
"k18", "k19", "k20", "k21", "k22", "k23", "k24", "k25",
"k26", "k27", "k28", "k29", "k30", "k31", "k32", "k33",
"k34", "k35", "k36", "k37", "k38", "k39", "k40", "k41",
"k42", "k43", "k44", "k45", "k46", "k47", "k48", "k49",
"k50", "k51", "k52"]

User avatar
Steamgirl
Veteran
Posts: 321
Joined: Sat Jul 28, 2012 4:39 am
Completed: My Cup of Coffee, Queen at Arms (co-wrote a battle scene)
Projects: Stranded Hearts, Emma: A Lady's Maid
Deviantart: steamgirlgame
Contact:

Re: "List index out of range" error, please help

#2 Post by Steamgirl » Tue Feb 23, 2016 10:00 am

This usually happens if you use an index that is either too high or too low for the list, which starts with 0 and ends with length - 1. But you probably already knew that.

I'm not entirely sure why it thinks it's out of range. Are you defining karty after you run this function? Does it still happen if you do:

$ karta5 = renpy.random.choice(["k1", "k2", "k3", "k4", "k5", "k6", "k7", "k8", "k9","k10", "k11", "k12", "k13", "k14", "k15", "k16", "k17","k18", "k19", "k20", "k21", "k22", "k23", "k24", "k25","k26", "k27", "k28", "k29", "k30", "k31", "k32", "k33","k34", "k35", "k36", "k37", "k38", "k39", "k40", "k41","k42", "k43", "k44", "k45", "k46", "k47", "k48", "k49","k50", "k51", "k52"])

?

Also, is renpy/python able to deal with multi-line lists?

User avatar
gas
Miko-Class Veteran
Posts: 718
Joined: Mon Jan 26, 2009 7:21 pm
Contact:

Re: "List index out of range" error, please help

#3 Post by gas » Tue Feb 23, 2016 6:39 pm

Out of index 'cause you're telling to python to remove karta5 value from the list karty.
If the value is "k11", it turn ASCII into decimal and so go search the 118th element (i think, chances is even higher. K=107 in ASCII).

Use a random number to define the index, and use/remove that item thank to the number.
If you want to debate on a reply I gave to your posts, please QUOTE ME or i'll be not notified about.

10 ? "RENPY"
20 GOTO 10

RUN

philat
Eileen-Class Veteran
Posts: 1759
Joined: Wed Dec 04, 2013 12:33 pm
Contact:

Re: "List index out of range" error, please help

#4 Post by philat » Tue Feb 23, 2016 7:23 pm

It's not because remove doesn't work with values (as opposed to indices). Likelier that the list isn't properly defined before the process.

User avatar
Hoyoka
Newbie
Posts: 4
Joined: Mon Feb 22, 2016 6:46 am
Projects: VN Princess of the Lost kingdom
Location: Slovakia
Contact:

Re: "List index out of range" error, please help

#5 Post by Hoyoka » Wed Feb 24, 2016 7:11 am

Thank you for your replies.
I tried to fix it and I already tried to put the list into one line , but it did not work.
I do not know what else should I try, but it is just a card game I tried to put to my visual novel and it is not necessary for the story, so I guess i should give up on it. Card games are just too difficult for me.
But, I am still very grateful for your answers.

User avatar
xela
Lemma-Class Veteran
Posts: 2481
Joined: Sun Sep 18, 2011 10:13 am
Contact:

Re: "List index out of range" error, please help

#6 Post by xela » Wed Feb 24, 2016 7:26 am

Hoyoka wrote:I do not know what else should I try
What have you tried thus far?

If you care about keeping the list ordered:

Code: Select all

$ karta = karty.pop(renpy.random.randrange(len(karty)))
if you don't care about that:

Code: Select all

$ renpy.random.shuffle(karty) # You can do this just once to randomize all items in list.
$ karta = karty.pop()
Like what we're doing? Support us at:
Image

User avatar
Steamgirl
Veteran
Posts: 321
Joined: Sat Jul 28, 2012 4:39 am
Completed: My Cup of Coffee, Queen at Arms (co-wrote a battle scene)
Projects: Stranded Hearts, Emma: A Lady's Maid
Deviantart: steamgirlgame
Contact:

Re: "List index out of range" error, please help

#7 Post by Steamgirl » Wed Feb 24, 2016 8:40 am

I used this code and it worked fine for me.

Code: Select all

    $ karty = ["k1", "k2", "k3"]
    $ karta5 = renpy.random.choice(karty)
    $ karty.remove(karta5)

    "[karty]"
The result is ["k1","k3"] or any random combination of 2, depending on which one was removed.

So I don't actually think its converting it to ascii or anything. In fact, having had a look at the official documentation for the two functions they are being used 100% correctly.

Furthermore, this code also works for me!

Code: Select all

    $ karty = ["k1", "k2", 
    "k3", "k4"]
    $ karta5 = renpy.random.choice(karty)
    $ karty.remove(karta5)
    
    "[karty]"
So it's not a multiline issue.

However, if I do this I get the index out of range issue.

Code: Select all

    $ karty = []
    $ karta5 = renpy.random.choice(karty)
    $ karty.remove(karta5)
    
    "[karty]"
Is it possible that once all the cards are removed it's still trying to remove another one?

I would make this modification to the code and see if that makes the error go away?:

Code: Select all

    if len(karty) > 0:
        $ karta5 = renpy.random.choice(karty)
        $ karty.remove(karta5)
    else:
        "There are no cards left in the list."

User avatar
Hoyoka
Newbie
Posts: 4
Joined: Mon Feb 22, 2016 6:46 am
Projects: VN Princess of the Lost kingdom
Location: Slovakia
Contact:

Re: "List index out of range" error, please help

#8 Post by Hoyoka » Thu Feb 25, 2016 5:01 am

Thank you for your advice.
I already made sure that when the list is already empty, it will jump to next turn.
I tried to change the code a bit and it is more simple now, but it does not work.
I changed it to something like this:
$ kar = renpy.random.choice(karty)
$ akarty.append(kar)
$ karty.remove(kar)

But I still get the same error "list index out of range" , it looks like there is some problem with the list named "karty".

User avatar
Steamgirl
Veteran
Posts: 321
Joined: Sat Jul 28, 2012 4:39 am
Completed: My Cup of Coffee, Queen at Arms (co-wrote a battle scene)
Projects: Stranded Hearts, Emma: A Lady's Maid
Deviantart: steamgirlgame
Contact:

Re: "List index out of range" error, please help

#9 Post by Steamgirl » Thu Feb 25, 2016 11:32 am

Hmm, when I use the code you pasted originally, it works fine for me. So the real problem is probably elsewhere. If you feel up for it, send me a PM with a download link to your project and I'll see if I can't debug it for you. I promise I won't steal/distribute anything. ;)

Post Reply

Who is online

Users browsing this forum: No registered users