+ 2

List index out of reach

Trying to choose random words from input and output them in a different order but i get this error import random inpt = input().split() for x in range(len(inpt)): out = "" wrd = random.randint(0, len(inpt)) out = inpt[wrd] + out print(out) Traceback (most recent call last) : File "..\Playground\", line 8, in <module> out = inpt[wrd] + out IndexError: list index out of reach

9th Jul 2018, 1:29 PM
Hampus Mathiesen
Hampus Mathiesen - avatar
3 Respuestas
+ 6
for x in range(len(inp)-1): wrd=randint(0,len(inpt)-1)
9th Jul 2018, 1:32 PM
Muhd Khairul Amirin
Muhd Khairul Amirin - avatar
+ 5
"random.randint(a, b)" includes b try random.randrange(0, len(inpt)) or random.randint(0, len(inpt) - 1)
9th Jul 2018, 5:05 PM
Mert Yazıcı
Mert Yazıcı - avatar
0
Keys for arrays start from 0. So the last key is the length minus 1. See below. There are fours words 0 1 2 3
9th Jul 2018, 1:41 PM
James
James - avatar