Skip to content
Advertisement

my diceroller is having issues with removing elements from the list, any suggestions?

i am trying to make a character statblock randomizer with a specific setup(because i am a nerd) where it basically “rolls a D6” 4 times and removes the lowest number. however when i try and remove the lowest number from the list, sometimes I get a “IndexError: pop index out of range” issue every 20 or so iterations. what am i doing wrong?

JavaScript

Advertisement

Answer

.pop() takes an item index, not the item to be removed. .remove() takes the item to be removed:

JavaScript

Output:

JavaScript

Note in the example above .pop(4) would generate an IndexError because valid indexes are 0-3 for 4-element list. .remove(4) removes the first 4 found in the list.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement