2

I have made the following piece of code to split N persons into random teams of 2, but in many cases, I get the error: pop index out of range.

Could someone help me?

from random import seed
from random import randrange
from datetime import datetime
seed(datetime.now())

N = int(input("How many students do you have?:"))

pupils = set()

for number in range(N):
    pupils.add(str(input('pupil name:')))
print(pupils)


teams = set()
lista = list(pupils)
for i in range(N//2):#// akerea dieresi
    pos1 = randrange(0, len(lista))
    pos2 = randrange(0, len(lista))
    pupil1 = lista.pop(pos1)
    pupil2 = lista.pop(pos2)
    team = (pupil1,pupil2)
    teams.add(team)

i = 0
for team in teams:
    i+=1
    print(team + str(i))
martineau
  • 119,623
  • 25
  • 170
  • 301
Evan karag
  • 21
  • 2
  • I presume both that `pos1` and `pos2` could be the index of the end of the list sometimes. – quamrana Sep 12 '20 at 15:14
  • 2
    The length of the list changes when you pop `pupil1`, what if `pos2` was `len(lista)-1`? – Thierry Lathuille Sep 12 '20 at 15:15
  • 2
    You should do a random shuffle of the whole list and then split off [pairs](https://stackoverflow.com/questions/312443/how-do-you-split-a-list-into-evenly-sized-chunks) – quamrana Sep 12 '20 at 15:16

2 Answers2

0

edit: this error means to say that you are trying to pop() from an index that is not on the list. maybe try to check if the list is already empty each time you try to take something out. I`ll use if lista to verify it is not empty.


teams = set()
lista = list(pupils)
for i in range(N//2):  #notice yo only have to do that N//2 times!
    if lista:                       #check if lista is empty
        pos1 = randrange(0, len(lista))
        pupil1 = lista.pop(pos1)
    if lista:
        pos2 = randrange(0, len(lista))
        pupil2 = lista.pop(pos2)

    team = (pupil1,pupil2)
    teams.add(team)

i = 0
for team in teams:
    i+=1
    print(team + str(i))
Nadavo
  • 250
  • 2
  • 9
  • No, it doesn't mean that the list is empty, but that there is no item in it at the index you're trying to `pop`. The OP's code as provided won't let the list get empty anyway. So, testing if the list isn't empty is useless. Furthermore, you are choosing a value in the list, and try to use it as an index in pop, which will cause an error. Please don't post wrong, untested code as answers. – Thierry Lathuille Sep 12 '20 at 15:35
0

Your immediate problem is you are shortening a list, but acting like removed indexes should still exist. Your bigger problem is that you are trying to invent your own random.shuffle(). Your code could be simplified greatly by using the builtin random.shuffle().

import random

#represents the results of your name input system
names = 'Tom Jerry Abbot Costello Regis Kelly Harry Sally Thelma Louise Solomon'.split()

#shuffle all the names
random.shuffle(names)

teams = []

#keep going as long as there are pairs
while len(names) > 1:
    teams.append({names.pop(), names.pop()})

#odd man out
if names:
    print(f"{names.pop()} can't play")

#results
print(*teams, sep='\n')
OneMadGypsy
  • 4,640
  • 3
  • 10
  • 26