I am trying to select a random element from my array.
However, sometimes I am getting an index out of bounds error, even though I accounted for the array starting at 0 by adding 1 to the maximum value of the index.
from random import randint
names = ['Sam', 'Paul', 'Mark', 'Simon', 'Sean', 'Samantha', 'Ellen']
random_name = names[randint(0, len(array)+1)]
print(random_name)
I only get an index out of bounds error sometimes?
why not just use choice?
import random
names = ['Sam', 'Paul', 'Mark', 'Simon', 'Sean', 'Samantha', 'Ellen']
random_name = random.choice(names)
print(random_name)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With