Reputation: 11
I need to make a program where the user inputs all students names (this is then converted into a list each name becomes an item) and y amount of teams, the program will then randomize the teams with the names of the students. Ex. The user chooses 2 teams and has 4 different students the program then spits out 2 randomized teams with 2 students in each (these cannot be the same students).
import random
import os
#clears the screen
os.system('cls')
#creates the list that later is filled with user input
list_name = []
temp_name = ""
#team_total is the user choosen amount of teams, this is then converted into integer
team_total = input("How many teams do you want?\n")
team_total = int(team_total)
#makes it so that all names written by the user are converted to individual list items
name = input("What are your students names?:\n")
for x in name:
if x == " ":
list_name.append(temp_name)
temp_name = ""
continue
else:
temp_name = temp_name + x
list_name.append(temp_name)
#using len to create varible that is equivilant to the amount of items in list_name
y = len(list_name)
#team_size is the amount of people in each team (amount of teams divided by amount of students)
team_size = (y / team_total)
print(f"There will be {team_size} per team!")
#r1 is the student in team_1
r1=random.randint(0,int(y))
team_a = (list_name[r1])
#r2 is the student in team_b (this also makes it so that r2 cant be the same as r1)
r2=random.randint(0,int(y))
while r2 == r1:
r2 = random.randint(0,(y))
r2=random.randint(0,int(y))
team_b = (list_name[r2])
This is how far I've gotten, I am completely stuck so help would be appriciated!
Upvotes: 0
Views: 87
Reputation: 27196
Once you have your players (a list of names) you can use random.shuffle() to rearrange the list into pseudo-random order. You can then access that list sequentially. For example:
from random import shuffle as SHUFFLE
nteams = int(input('How many teams? '))
players = input('Enter player names separated by space: ').split()
SHUFFLE(players)
ts = len(players) // nteams
teams = {i//ts: players[i:i+ts] for i in range(0, len(players), ts)}
print(teams)
Example:
How many teams? 3
Enter players names separated by space: john david paul peter clive boris fred
{0: ['paul', 'boris'], 1: ['john', 'david'], 2: ['clive', 'fred'], 3: ['peter']}
Note:
Note what happens when the number of players is not exactly divisible by the number of teams
Upvotes: 0
Reputation: 291
list.pop()
is an easy way here:
import random
players = ["Anton", "Bernd", "Charlie", "Dickins", "Eugine", "Franco"]
teams = {}
num_teams = 5 # This is what you would ask the user
def run():
for team_number in range(num_teams):
teams.update({team_number: []})
while len(players) > 0:
for team_number, team in teams.items():
if len(players) == 0:
break
index_to_remove = random.randint(0, len(players) - 1)
team.append(players.pop(index_to_remove))
print(teams)
You have a player list players = ["Anton", "Bernd", "Charlie", "Dickins", "Eugine", "Franco"]
A teams
dictionary. This dictionary you populate with the amount of teams (empty lists []
) that you need:
for team_number in range(num_teams):
teams.update({team_number: []})
After that you go over each team in the dictionary, pop()
a random player from your players
list into that team.
Repeat this process until you have no more players in the players
list.
If you have more players than can be equally divided among the teams, the first teams in the dictionary will have a higher amount of players than the later teams. If you insist on equal teams, you need to insert a check that the players can be equally divided among all teams. If that check fails the program aborts:
len(players) % num_teams == 0
Sample output for 3 teams:
How many teams do you want?
3
{0: ['Anton', 'Dickins'], 1: ['Eugine', 'Franco'], 2: ['Bernd', 'Charlie']}
additional information: https://docs.python.org/3/tutorial/datastructures.html
Upvotes: 1