我想创建一个类,获得一个名称并将其设置为 A 队和 B 队之间的随机团队,然后将其找出来,但需要由 22 个人组成,每队 11 人,我怎样才能使随机不给我更多我想要 python 中的数量? 我制作了这个程序,但每个程序被选择的最大次数并不是随机的:
import random
class human:
count = 0
def __init__(self,name):
self.name = name
class fotbalist(human):
def get_name(self):
team = ['A','B']
self.team = random.choice(team)
print('%s is in %s team'%(self.name,self.team))
尝试此代码,如果遇到任何错误请告诉我,它还包括解释。
import random
# Set the total number of people and maximum allowed per team
total_people = 22
max_per_team = 11
# Randomly choose the number of people on team A (between 0 and max_per_team)
num_team_a = random.randint(0, max_per_team)
# Assign the remaining people to team B
num_team_b = total_people - num_team_a
# Create lists of 'A' and 'B', repeated for the number of people on each team
team_a = ['A'] * num_team_a
team_b = ['B'] * num_team_b
# Combine the team lists into one list of all people
all_people = team_a + team_b
# Randomly shuffle the order of people
random.shuffle(all_people)
# Print out each person's team assignment
for person in all_people:
print(person)