Zozo
Zozo

Reputation: 9

Python: "list index out of range" when attempting to do a loop that chooses random numbers

My task is to randomize cards with a name and four categories, and to bring them out from a text file and save the completed cards back to another text file. The number of cards randomized is up to user input, except it will only loop a few times before displaying this error.

  line 35, in <module>
  exercise = lines5[exerlinenum]
IndexError: list index out of range

Here is the code I am using.

x = 0
list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 
20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30]
user = int(input("Please enter your even number: "))
if user % 2 == 0:
     answer = list.index(user)
     split_list = list[0:answer+1]
     loop = len(split_list)

else:
     print("Input not even!")
     import random
     while x < loop:

         file = open("dognames.txt", "r")
         lines1=file.readlines()
         namelinenum=random.randint(0, 17)
         dogname = lines1[namelinenum]
         print("Name:", dogname)

         file2 = open("friendliness.txt", "r")
         lines2=file2.readlines()
         frenlinenum=random.randint(0, 10)
         friendliness = lines2[frenlinenum]
         print("Friendliness:", friendliness)

         file4 = open("intelligence.txt", "r")
         lines4=file4.readlines()
         intlinenum=random.randint(0, 100)
         intelligence = lines4[intlinenum]
         print("Intelligence:", intelligence)

         file5 = open("exercise.txt", "r")
         lines5=file5.readlines()
         exerlinenum=random.randint(0, 5)
         exercise = lines5[exerlinenum]
         print("Exercise:", exercise)

         file6 = open("drool.txt", "r")
         lines6=file6.readlines()
         droollinenum=random.randint(0, 10)
         drool = lines6[droollinenum]
         print("Drool:", drool)

         doginfo=(dogname, friendliness, intelligence, exercise, drool)
         file3 = open("dogs.txt", "a")
         file3.write(repr(doginfo))



         x += 1

    file3.close()

And here are the text files in question, if that helps.

dognames.txt

 Jack
 Harry
 Lyla
 Lucky
 Ringo
 Sadie
 Pinkie
 Teddy
 Simrin
 Andie
 Willow
 Luna
 Bailey
 Zoey
 Checkers
 Cheddar
 Arlo
 Humphrey

friendliness.txt

1
2
3
4
5
6
7
8
9
10

intelligence.txt

1
2
3
4
5
6
7
8
9
10
11 
12 
13 
14 
15 
16 
17 
18 
19 
20 
21 
22 
23 
24 
25 
26 
27 
28 
29 
30 
31 
32 
33 
34 
35 
36 
37 
38 
39 
40 
41 
42 
43 
44 
45 
46 
47 
48 
49 
50 
51 
52 
53 
54 
55 
56 
57 
58 
59 
60 
61 
62 
63 
64 
65 
66 
67 
68 
69 
70 
71 
72 
73 
74 
75 
76 
77 
78 
79 
80 
81 
82 
83 
84 
85 
86 
87 
88 
89 
90 
91 
92
93
94 
95
96
97 
98
99
100

exercise.txt

1
2
3
4
5

drool.txt

1
2
3
4
5
6
7
8
9
10

Thanks a lot, and please go easy on me. I'm a beginner.

Upvotes: 0

Views: 1130

Answers (4)

KenHBS
KenHBS

Reputation: 7174

You are trying to access on element of a list called lines5. This list contains 5 elements that are read from exercise.txt.

In python, counting starts at 0, so if you want to access the first element of a list, you use lines5[0]. If you extend this logic, you will see that lines5[4] gives you the 5th element of lines5, and lines5[5] would try to access the 6th element, which does not exist in your list.

Then, your code example uses random.randint(0, 5) to pick a random number. This random number is used to access an element of lines5. random.randint(0, 5) may return 0, 1, 2, 3, 4 or 5! If it happens to return a 5, you will be hit with

IndexError: list index out of range

because lines5 only contains 5 elements, and you are trying to access the 6th element!

You can solve your problem by using random.randint(0, 4) are adding an additional line to exercise.txt

Upvotes: 1

WONderFUL
WONderFUL

Reputation: 66

The other answer tells the reason why you get the error - due to the random library function return values. More generic, to get rid of the "list index out of range" error, it is safe to check the index before accessing the list item. For instance,

if line5: # make sure the list is not empty
    if exerlinenum >= len(line5): # index out of range
        # throw error or what ever
    else:
        exercise = lines5[exerlinenum]

Upvotes: 0

Jay
Jay

Reputation: 2471

The function random.randint(a,b) returns a random number inclusive of both a and b. So in your case, it can return a random number between 0 and 5.

In python and most of the languages, the list indexes start from 0. So your lines5 list size is 5, having indexes from 0 to 4. Please change line 35 to this -

exerlinenum=random.randint(0, 4)

And it should work without any issue. You also need to reduce the second argument by 1 of all random.randint() calls so that you don't see this error.

Upvotes: 2

dhae
dhae

Reputation: 115

The problem is that if exerlinenum=random.randint(0, 5) is 5, then the index is out of range since the exercise.txt just has 5 entries. (Remember, python starts counting indexes with 0)

Looking at your other lines you have the same problem. (e.g. intelligence does not have 101 entries but only 100.) You most probably only see it with exercise since the probability of getting 5 out of 6 is bigger than getting a 100 out of 101 possibilities.

Upvotes: 1

Related Questions