Home > Software design >  Show duplicate results in Python
Show duplicate results in Python

Time:11-14

My code:

import random

class human:
    'humans class'
    def __init__(self,fname,lname=None,age=None):
        self.fname = fname
        if lname != None: self.lname = lname 
        else: self.lname = None
        if age != None: self.age = age 
        else: self.age = None

    def new(self,command):
        "for define a new variable"
        return exec(command)


class soccer_player(human):
    "soccer players class"
    
    all_players = []
    choised = []
    a = []
    b = []
    def __init__(self, fname, lname=None, age=None):
        super().__init__(fname, lname=lname, age=age)
        soccer_player.all_players.append(fname)
    
    def start_random_choise():
        i=0
        while True:
            choise = random.choice(soccer_player.all_players)
            if choise not in soccer_player.choised:
                soccer_player.a.append(choise)
                i = i   1
            if i == 11:
                break
        i=0
        while True:
            choise = random.choice(soccer_player.all_players)
            if choise not in soccer_player.choised:
                soccer_player.b.append(choise)
                i = i   1
            if i == 11:
                break

hosein = soccer_player("hosain")
maziar = soccer_player("maziar")
akbar = soccer_player("akbar")
nima = soccer_player("mina")
mehdi = soccer_player("mehdi")
farhad = soccer_player("farhad")
mohammad = soccer_player("mohammad")
khashaiar = soccer_player("khashaiar")
milad = soccer_player("milad")
mostafa = soccer_player("mostafa")
amin = soccer_player("amin")
saeed = soccer_player("saeed")
poya = soccer_player("poya")
porya = soccer_player("porya")
reza = soccer_player("reza")
ali = soccer_player("ali")
behzad = soccer_player("behzad")
sohail = soccer_player("sohail")
behrooz = soccer_player("behrooz")
shahrooz = soccer_player("shahrooz")
saman = soccer_player("saman")
mohsen = soccer_player("mohsen")

soccer_player.start_random_choise()

print("A:",end=" ")
for i in list(range(len(soccer_player.a))):
    print(soccer_player.a[i],end=" , ")
print()
print("B:",end=" ")
for i in list(range(len(soccer_player.b))):
    print(soccer_player.b[i],end=" , ")
print()

My problem is this app shows duplicate results:

[nimafanniasl@fedora prs]$ python -u "/home/nimafanniasl/pr.py"
A: mehdi , porya , maziar , porya , akbar , porya , behzad , amin , khashaiar , maziar , ali , 
B: hosain , mohammad , mostafa , shahrooz , mostafa , poya , mostafa , hosain , farhad , hosain , hosain ,

This app printed 4 hesein in team B, Please help me to make this program so that it does not show duplicate results

CodePudding user response:

You didn't add the chosen players into choised. Add this line

soccer_player.choised.append(choise)

in both while loops.

CodePudding user response:

  1. Your if condition is broken because you check the wrong list. You append players to soccer_player.a.append(choise) or b but your condition states if choise not in soccer_player.choised. You probably meant to append the players to the choised list, too.

  2. You can avoid doing all this work if you replace most of this code with random.sample or random.shuffle which already guarantee you that they do not produce duplicates.

    def start_random_choise():
        soccer_player.a = random.sample(soccer_player.all_players, 11)
        soccer_player.b = random.sample(soccer_player.all_players, 11)
  1. Please look up "birthday paradox" on why your code is inefficient
  • Related