Maxy Picky
Maxy Picky

Reputation: 31

Moving a String from one list to another

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?

Upvotes: 0

Views: 1060

Answers (3)

Cody Stevens
Cody Stevens

Reputation: 414

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])

Upvotes: 0

LittleQ
LittleQ

Reputation: 1925

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

Upvotes: -1

Red Shift
Red Shift

Reputation: 1312

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'.

Upvotes: 4

Related Questions