I don't understand this error | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
11th Oct 2020, 4:43 PM
Royalx
Royalx - avatar
9 Answers
+ 7
here is the reworked code: - corrected index calculation - removed multiple import of random https://code.sololearn.com/cSd8Gi01M9Ip/?ref=app
11th Oct 2020, 5:18 PM
Lothar
Lothar - avatar
+ 7
Royalx , i saw that is working now. Great! 👍
12th Oct 2020, 4:51 PM
Lothar
Lothar - avatar
+ 6
Royalx , you need to post the code here. best way is to put it in playground and then link it in your post. without having seen the complete code, everything is a guess. Thanks!
11th Oct 2020, 4:47 PM
Lothar
Lothar - avatar
+ 5
I tried using this and it worked when the index is less than len(av) but is out of range when it equals len. It will work if you say len(av)-1 https://code.sololearn.com/cZMsz69GJ5Yn or just use print(r.choice(av))
12th Oct 2020, 5:16 PM
David Ashton
David Ashton - avatar
+ 3
Royalx Simple example:- Consider a list of length 5 List=[4,5,7,8,6] Pos- 0 1 2 3 4 your function/program calling list value of index/pos less than 0 or greater than 4 (ex:- List[6]).
11th Oct 2020, 4:51 PM
Ajith
Ajith - avatar
+ 3
Thanks :)
11th Oct 2020, 4:54 PM
Royalx
Royalx - avatar
+ 1
This mean that the index given as input to av list is great than its lenght if there are 5 element in your av list so r.randint(0,len(av)) will generate a number great than 5 .
11th Oct 2020, 4:50 PM
HBhZ_C
HBhZ_C - avatar
+ 1
import random as r for i in range(10): print(r.randint(0,5)) Prints number 5 as well,so you need to pass len(av)-1 as second argument to randint
11th Oct 2020, 4:51 PM
Abhay
Abhay - avatar