0

Whenever i run the programme, it comes up with either errors

Traceback (most recent call last):
  File "C:\Users\mrosales\Downloads\Rock Paper Sissor Tornament.py", line 46, in <module>
    Temp = ClassList[Random2]
IndexError: list index out of range

Traceback (most recent call last):
  File "C:\Users\mrosales\Downloads\Rock Paper Sissor Tornament.py", line 60, in <module>
    Temp = ClassList[Random2]
IndexError: list index out of range

My code is about moving strings from one list to another to form set games of a tornamnet

import random
import time
Temp = (" ")
ClassList = ['Noah','Simone','Ji Ho','Thanh','Nathanial','Soo','Mickel','Tuan']
Match1 = [], Match2 = [], Match3 = [] ,Match4 = []
Random1 = random.randrange(0,len(ClassList))
Random2 = random.randrange(0,len(ClassList))
while Random1 == Random2:
    Random1 = random.randrange(0,len(ClassList))

time.sleep(1)
Temp = ClassList[Random1]
Match1.append(Temp)
del ClassList[Random1]
Temp = ClassList[Random2]
del ClassList[Random2]
Match1.append(Temp)

print(Match1)
Random1 = random.randrange(0,len(ClassList))
Random2 = random.randrange(0,len(ClassList))
while Random1 == Random2:
    Random1 = random.randrange(0,len(ClassList))

time.sleep(1)
Temp = ClassList[Random1]
Match2.append(Temp)
del ClassList[Random1]
Temp = ClassList[Random2]
del ClassList[Random2]
Match2.append(Temp)

print(Match2)
Random1 = random.randrange(0,len(ClassList))
Random2 = random.randrange(0,len(ClassList))
while Random1 == Random2:
    Random1 = random.randrange(0,len(ClassList))

time.sleep(1)
Temp = ClassList[Random1]
Match3.append(Temp)
del ClassList[Random1]
Temp = ClassList[Random2]
del ClassList[Random2]
Match3.append(Temp)

print(Match3)
Random1 = random.randrange(0,len(ClassList))
Random2 = random.randrange(0,len(ClassList))
while Random1 == Random2:
    Random1 = random.randrange(0,len(ClassList))

time.sleep(1)
Temp = ClassList[Random1]
Match4.append(Temp)
del ClassList[Random1]
Temp = ClassList[Random2]
del ClassList[Random2]
Match4.append(Temp)

print(Match4)
Random1 = random.randrange(0,len(ClassList))
Random2 = random.randrange(0,len(ClassList))
while Random1 == Random2:
    Random1 = random.randrange(0,len(ClassList))

print ("The current match ups are...")
print (Temp)
time.sleep(1)
print (Match1, Match2, Match3, Match4)

Can anyone spot an error I may have placed and, if they wish, correct it?

Maxy Picky
  • 31
  • 1
  • 8
  • 1
    Post the full traceback? which line is causing the error ? – ZdaR Jun 24 '15 at 03:24
  • Have you tried putting some break points in and stepping through? You would probably be able to find what the issue is. – Red Shift Jun 24 '15 at 03:28
  • @ZdaR its kind of a long code, if i edit my post and input the full code, it'll fill more than a screen page... you sure about that? I wouldn't mind as long as you confirm – Maxy Picky Jun 24 '15 at 03:31
  • It doesn't have an obvious stopping point. If I had to guess, what's happening is that classlist empties, and the random number still produces 0---then tries to find index 0 of the empty list, and kablooie. (Just a guess, I'm not totally sure what trying to access index 0 of an empty list does). If my guess is right, then wrapping the whole thing in a while len(classlist) != 0 block should do the trick. (Edit: nevermind, that doesn't work. meh.) – Paul Gowder Jun 24 '15 at 03:31
  • @RedShift Could you explain what a break point is? Preferably with examples – Maxy Picky Jun 24 '15 at 03:43
  • A breakpoint is: https://en.wikipedia.org/wiki/Breakpoint. Using it with Python can be found here: http://stackoverflow.com/questions/6980749/simpler-way-to-put-pdb-breakpoints-in-python-code. The IDE I personally use, that also supports breakpoints (part of debugging), is called PyCharm: https://www.jetbrains.com/pycharm/ – Red Shift Jun 24 '15 at 03:53

3 Answers3

4

From what I gather your code is trying to do, I believe the below code will achieve it. It's quite a bit shorter, but basically does what you were doing without the crashes.

import random

ClassList = ['Noah', 'Simone', 'Ji Ho', 'Thanh', 'Nathanial', 'Soo', 'Mickel', 'Tuan']

# Randomise list order
random.shuffle(ClassList)

# Remove last 2 elements from list and add to new match lists
Match1 = [ClassList.pop(), ClassList.pop()]
Match2 = [ClassList.pop(), ClassList.pop()]
Match3 = [ClassList.pop(), ClassList.pop()]
Match4 = [ClassList.pop(), ClassList.pop()]

print(Match1, Match2, Match3, Match4)

Note, if you run this multiple times you will see that it does indeed give different 'match fixtures'.

Red Shift
  • 1,312
  • 2
  • 17
  • 29
0

You are modifying your class list as you iterate through it. When it throws the error there is nobody left in your class to pick len(ClassList) 0 .

If I understand what you are trying to do you could just create another list and append names to it as you iterate through your original list to keep track of who has been matched. Or even better use random.shuffle

import random
import time
classlist = ['Noah','Simone','Ji Ho','Thanh','Nathanial','Soo','Mickel','Tuan']
from random import shuffle

def get_random( classlist ):
  shuffle( classlist )
  while classlist:
    yield classlist.pop()

matches = []
match = []

for player in get_random(classlist):
    if len(match) <= 1:
        #print "Adding %s" %player
        match.append(player)
        if len(match) == 2:
            matches.append(match)
            #print "Match %s is full" %len(matches)
            match = []

for x in range(0,len(matches)):
    print "Match %s: %s" %(x,matches[x])
Cody Stevens
  • 424
  • 4
  • 9
-1

Solution 1:
del ClassList[Random1] after second Temp assignment.

Solution 2:
Random2 = random.randrange(0,len(ClassList)-1) will fix your problem, and then you don't need Random1 != Random2 in your case.

What's more:
You should clean up your code ...

temp = classList.pop(random1)

is equivalent to

temp = classList[random1]
del classList[random1]

Use a function to take care of the duplicate code:

def get_random():
    random_index = random.randrange(0,len(classList))
    return classList.pop(random_index)

match1 = [get_ramdom(), get_random()]

Do NOT use capitalized variable name according to PEP8

LittleQ
  • 1,860
  • 1
  • 12
  • 14
  • This does not fix the problem. The issues arise because of the 'del' keyword and it changing list lengths. In turn, allowing once valid index positions to become invalid. – Red Shift Jun 24 '15 at 04:27
  • @RedShift make `len(ClassList)-1` would fix the random to match the **new list length** – LittleQ Jun 24 '15 at 04:28