List Index Out of Range on Python. Nothing works -
i have reviewed multiple threads similar answers question. nothing seems working no matter try.
i trying create 100 random numbers, , put random numbers list. keep getting
file "e:\workingwithfiles\funstuff.py", line 17, in randnumbs numblist[index]+=1 indexerror: list index out of range my code is:
def randnumbs(numbcount): numblist=[0]*100 i=1 while < 100: index = random.randint(1,100) numblist[index]+=1 i+=1 print (numblist) return (numblist) after reviewing multiple threads , tinkering around cannot seem answer.
before continue here scope of project: have .txt file thats dictionary many words in it. first, write function calculate how many words in .txt file. second, generate 100 random numbers between 1 , amount of words in .txt file. lastly need create .txt file prints
"number word" 120 bologna and on. having trouble generating random numbers. if has idea on why list index out of range , how help, appreciated! thank you!
edit: .txt file 113k words long
you made list of size 100 here:
numblist=[0]*100 your problem create indexes 1 100 when should accessing indexes 0-99. given list of size n, valid list indexes 0 n-1
change code to
index = random.randint(0,99)
Comments
Post a Comment