+ 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
3 Antworten
+ 6
for x in range(len(inp)-1):
wrd=randint(0,len(inpt)-1)
+ 5
"random.randint(a, b)" includes b
try random.randrange(0, len(inpt))
or random.randint(0, len(inpt) - 1)
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