Home > Blockchain >  OOP and use of random library to divide footballist between two teams
OOP and use of random library to divide footballist between two teams

Time:08-12

I want to have a class of People and a class of Footballists that inherit from people. I have 22 persons that I must divide between two teams of A and B without overlap. I must also divide people between two classes randomly. I am very new in OOP and here is my code that has overlap. Could you help me?

import random
listt=['Hossien','Maziyar','Akbar','Nima','Mahdi','Farhad','Mohammad','Khashayar','Milad','Mostafa','Amin','Saeeid','Poya','Poriya','Reza','Ali','Behzad','Sohiel','Behrooz','Shahrooz','Saman','Mohsen']
#print(listt)
 class Person:

    def __init__(self):
        self.nameList=list()
    
 class Footbalist(Person):
    def __init__(self,team):
        self.team = team

    def get_team(self):
        self.teamA=list(random.sample(listt,11))
        self.teamB=[x for x in listt if x not in self.teamA]


    def printteamA(self):
        print("team A :",self.teamA)
    
    def printteamB(self):
        print("team B :",self.teamB)

A=Footbalist('A')
A.get_team()

B=Footbalist('B')
B.get_team()

A.printteamA()
B.printteamB()

CodePudding user response:

You need use super to inherit Footbalist from Person. And each A and B is an instance of Footbalist

import random
listt=['Hossien','Maziyar','Akbar','Nima','Mahdi','Farhad','Mohammad','Khashayar','Milad','Mostafa','Amin','Saeeid','Poya','Poriya','Reza','Ali','Behzad','Sohiel','Behrooz','Shahrooz','Saman','Mohsen']
#print(listt)
class Person:

    def __init__(self):
        self.nameList=list()

class Footbalist(Person):
    def __init__(self,team_name):
        super().__init__()
        self.team_name = team_name

    def get_team(self, other_team=None):
        if other_team == None:
            self.nameList = list(random.sample(listt, 11))
        else:
            self.nameList=[x for x in listt if x not in other_team.nameList]


    def printteam(self):
        print(f"team {self.team_name}: {self.nameList}")

A=Footbalist('A')
A.get_team()

B=Footbalist('B')
B.get_team(A)

A.printteam()
B.printteam()
  • Related