1

Started working with Python today and decided to test my learnings on a small project.

I want to have a "roster" of, lets say 12 people. From this roster, I want there to be "x" number of teams of "y", depending on how many people want to play. I know how to write this, if we assume all names in the roster are going to be used, but where I'm lost is how to generate unique teams for a composition of something like 3 teams of 2. Whenever I try to solve this, I will get 3 of the same teams of 2.

Here is an example of the code I'm attempting

import random

names = ["Tom", "Sam", "Jake", "McKenzie", "Sarah", "Devon", "Brice", "Caleb", "Mitchell", "Scott", "Nick", "Liz"]

teams = int(input("How many teams will there be? "))
players = int(input("How many players will each team have? "))

remaining_teams = teams
item = random.sample(names, k=players)


while remaining_teams > 0:
    print(item)
    remaining_teams = remaining_teams - 1
    if remaining_teams < 1:
        break

Right now, if my input variables were 4 and 2, I would get something returning like:

["Sam", "Tom"]
["Sam", "Tom"] 
["Sam", "Tom"] 
["Sam", "Tom"] 

How can I change this to get random results closer to this?:

["Sam", "Tom"]
["Nick", "Sarah"]
["Devon", "Jake"]
["Mitchell, "Liz"]
0

3 Answers 3

1

Try to put this line

item = random.sample(names, k=players)

inside the while loop:

while remaining_teams > 0:
    item = random.sample(names, k=players)
    print(item)
    remaining_teams = remaining_teams - 1

There is no need to check if remaining_teams is less than 0 because if it is zero it will never enter the loop so it won't get lower.

Sign up to request clarification or add additional context in comments.

Comments

0

You can also just use a for loop:

import random

names = ["Tom", "Sam", "Jake", "McKenzie", "Sarah", "Devon", "Brice", "Caleb", "Mitchell", "Scott", "Nick", "Liz"]

teams = 4 # int(input("How many teams will there be? "))
players = 2 # int(input("How many players will each team have? "))

for i in range(teams):
    item = random.sample(names, k=players)
    names.remove(item[0])
    names.remove(item[1])
    print(item)

1 Comment

Thanks for the alternative suggestion! I thought about using one, and initially started writing with this, but I think because I have really no programming experience, the syntax of While was just simpler for my mind to problemsolve.
0

You just aren't ever re-sampling the names to construct a new team. When you have item = random.sample(names, k=players) outside the loop, that code is only going to run once, not every time you subsequently reference/print item. You should put it in the loop so that every team is a new sample of names. Also, you might want to remove names from the names list once they're added to a team (so players can't show up on multiple teams). You could do this with another simple for loop inside your while loop. Finally, the last break statement is unnecessary: the while loop will automatically stop when remaining_teams hits 0, so no need to check and break if its less than 1.

You could also check to make sure you have enough names to fill the teams you request (so that you don't have a runtime error if, for instance, 3 teams of 15 players are requested, but you only have 15 names).

Here are all those changes altogether:

import random

names = ["Tom", "Sam", "Jake", "McKenzie", "Sarah", "Devon", "Brice", "Caleb", "Mitchell", "Scott", "Nick", "Liz"]

teams = int(input("How many teams will there be? "))
players = int(input("How many players will each team have? "))

if teams * players > len(names):
  print("Not enough players")
  exit()

remaining_teams = teams

while remaining_teams > 0:
    team = random.sample(names, k=players)
    print(team)
    for player in team:
      names.remove(player)
    remaining_teams = remaining_teams - 1

3 Comments

Awesome, I appreciate all the insight! I think I even thought about that, but at 2:00am after learning all day my brain was just fried and couldn't come up with the solution haha. Doesn't help that I have zero programming history other than what I learned yesterday. Thanks for the help and explanations, leaving the majority of my code the same!
Also, say I wanted to make a small application to run this on my desktop through a simple interface. What direction should I start researching to do that? Do I need to learn a framework? Can I use something like Tkinter? Is it probably too complex for me right now and should I just go back to basics? haha
@KeatonGarner No problem! Making graphical user interfaces might be a little hard to get into just yet, but Tkinter would be the simplest way to get started with that in python. I would just search for a Tkinter tutorial online (YouTube or text, up to you) and try to program along with it. Change stuff, break stuff, read docs to figure out why it’s broken or to learn about what other options you have, repeat. Good luck!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.