vendredi 22 septembre 2017

Python - Soccer Team allocation: Randomly select a score from a .txt file AND ensure it doesn't get selected again

I am a school teacher and I've been working on a way to select even soccer teams for an entire year level.

-Figuring out how to make even-teams isn't the problem-

What I have so far randomly selects 4 students on offence, 4 students on defence, 1 goalkeeper, & 1 coach (teacher) After which, it displays the teams average score. Please see current code below:

import random


numberofteams = int(input('How many teams? '))
for x in range(numberofteams): 



    offence = open('offence.txt','r').read()
    offence = offence.split('\n')

    for i in range(len(offence)):
        offence[i] = offence[i].split(', ')
        offence[i][1] = float(offence[i][1]) * 1000

    def offencelist(offencelist):
        offence1 = offencelist.pop(random.randint(0,len(offencelist)-1))
        offence2 = offencelist.pop(random.randint(0,len(offencelist)-1))
        offence3 = offencelist.pop(random.randint(0,len(offencelist)-1))
        offence4 = offencelist.pop(random.randint(0,len(offencelist)-1))

        offencescore = (offence1[1] + offence2[1] + offence3[1] + offence4[1])/4000

        return offence1[0], offence2[0], offence3[0], offence4[0], offencescore


    print(offencelist(offence))



    defence = open('defence.txt','r').read()
    defence = defence.split('\n')

    for i in range(len(defence)):
        defence[i] = defence[i].split(', ')
        defence[i][1] = float(defence[i][1]) * 1000

    def defencelist(defencelist):
        defence1 = defencelist.pop(random.randint(0,len(defencelist)-1))
        defence2 = defencelist.pop(random.randint(0,len(defencelist)-1))
        defence3 = defencelist.pop(random.randint(0,len(defencelist)-1))
        defence4 = defencelist.pop(random.randint(0,len(defencelist)-1))

        defencescore = (defence1[1] + defence2[1] + defence3[1] + defence4[1])/4000

        return defence1[0], defence2[0], defence3[0], defence4[0], defencescore

    print(defencelist(defence))



    goalkeeper = open('goalkeeper.txt','r').read()
    goalkeeper = goalkeeper.split('\n')

    for i in range(len(goalkeeper)):
        goalkeeper[i] = goalkeeper[i].split(', ')
        goalkeeper[i][1] = float(goalkeeper[i][1]) * 1000

    def goalkeeperlist(goalkeeperlist):
        goalkeeper1 = goalkeeperlist.pop(random.randint(0,len(goalkeeperlist)-1))

        goalkeeperscore = (goalkeeper1[1])/1000

        return goalkeeper1[0], goalkeeperscore

    print(goalkeeperlist(goalkeeper))



    coach = open('coach.txt','r').read()
    coach = coach.split('\n')

    for i in range(len(coach)):
        coach[i] = coach[i].split(', ')
        coach[i][1] = float(coach[i][1]) * 1000

    def coachlist(coachlist):
        coach1 = coachlist.pop(random.randint(0,len(coachlist)-1))

        coachscore = (coach1[1])/1000

        return coach1[0], coachscore

    print(coachlist(coach))
    print('----------------------------------------------')

The big problem right now is that students are currently showing up in 2 or even more teams which is obviously impossible and I have no idea how to fix this. I think I need to keep random.randint as I will eventually want to do calculations with the student's scores.

The code gets the student data from individual .txt files (I've purposely sorted offence & defence by gender to make it easier to spot errors - please see below:

Offence:

Harry, .984
Ben, .982
Tom, .981
Sam, .979
Kyle, .969
Shaun, .965
Louis, .963
Bob, .962
Bill, .959
Robert, .959

Defence:

Olivia, 1.0
Stephanie, .995
Kate, .989
Lucy, .987
Heather, .983
Sarah, .979
Elizabeth, .975
Besty, .974
Jan, .972

Goalkeeper:

David, .988
Emily, .985
Amelia, .982
Annabelle, .979
Michael, .976

Coach:

Denham, .991
Gardner, .989
Nevett, .985
Rogers, .981
Green, .979

I apologise for both the long code as well as the long question. But if anyone could help I will be in genuine awe.

Thanks very much




Aucun commentaire:

Enregistrer un commentaire