Search code examples
pythonrandomindexingrangeout

Getting "IndexError: list index out of range"


I'm wondering why this error comes up, IndexError: list index out of range. If the full program is required then I will upload, but the error is in this part of the code.

import random
Sign = ["+-*"]
num = int(random.random()*2)
operator = (Sign[num])
digit = int(random*10)

This is meant to output a random sign of the array.


Solution

  • Your list only contains one element. Try this:

    Sign = ["+", "-", "*"]