-1

I'm trying to make a function that will take a string as an input, determine how many teams and the players in the list and sort the players randomly into teams. I'm using a discord bot for context, but it isn't important here.

I've tried to associate a number with the corresponding team's empty list, but dictionaries won't allow lists in them and I don't know how else two associate two values.

def PickTeam(string):
    teamNum = number_of_teams
    PlayerList = just_assume_this_is_a_good_list
    team1 = []
    team2 = []
    team3 = []
    team4 = []
    teamDict = { 1:team1 , 2:team2 , 3:team3 }
    while teamNum > 0:
        for i in teamNum:
            addPlayer = choice(PlayerList)
            addTeam = teamDict[i]
            addTeam.append(addPlayer)
    for i in teamNum:
        teamPub = teamDict[i]
        print(teamPub)

I want to be able to input a number of teams and a list of names, like "2", and "Billy, Susie, Frank, Luke, Kensie, Jack" and have the output say something like "Billy, Luke, Susie", "Frank, Jack, Kensie", but there just isn't an output right now as the scripts stops at the dictionary holding lists. Any help would be appreciated, thanks!

TechSloth
  • 11
  • 5
  • What do you mean by "dictionaries won't allow lists in them"? Dictionary values can be any type of data. – Barmar Apr 19 '19 at 00:15
  • You'll be able to get more help if you post a working code sample (at least up to the part you're confused by) - so someone can paste it into their editor and see the same error as you. – perigon Apr 19 '19 at 00:16
  • What is `for i in teamNum:` supposed to mean? `teamNum` is a number, not something you can iterate over. Do you mean `for i in range(teamNum):`? – Barmar Apr 19 '19 at 00:16
  • Certainly a dictionary can accept lists as values. You can't use lists as keys because they can't be hashed, but you can use Tuples, which can be. – CryptoFool Apr 19 '19 at 00:16
  • `while teamNum > 0:` is an infinite loop, since you never do anything to reduce it in the body. Did you mean `if teamNum > 0:`? – Barmar Apr 19 '19 at 00:18
  • Welcome to StackOverflow. Please provide a [Minimal, Complete, and Verifiable Example](https://stackoverflow.com/help/mcve), and also review [How to ask](https://stackoverflow.com/help/how-to-ask) – CryptoFool Apr 19 '19 at 00:21
  • What is the `string` input for? You never use it. – Barmar Apr 19 '19 at 00:25

1 Answers1

1

You seem to have some issues with basic Python syntax, like if versus while, and how to loop N times.

In addition, there doesn't seem to be any need for a dictionary. You can just use a list of lists.

See Python - randomly partition a list into n nearly equal parts for simple ways to randomly sort the players into N teams.

def PickTeam(number_of_teams, player_names):
    PlayerList = player_names.split(", ")
    random.shuffle(PlayerList)
    teams = [PlayerList[i::number_of_teams] for i in range(number_of_teams)]
    return teams

for team in PickTeam(2, "Billy, Susie, Frank, Luke, Kensie, Jack"):
    print(team)

In the list comprehension, PlayerList[i::number_of_teams] is a slice of the PlayerList list that stars from element i, goes to the end of the list, taking every number_of_teams element. So if number_of_teams is 3, it will create a list of 3 sublists. The first sublist will be:

[PlayerList[0], PlayerList[3], PlayerList[6], ...]

the second will be:

[PlayerList[1], PlayerList[4], PlayerList[7], ...]

and the third will be:

[PlayerList[2], PlayerList[5], PlayerList[8], ...]

DEMO

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Running the code, directly copying and pasting, I receive the error: "TypeError: PickTeam() takes 1 positional argument but 2 were given", and I am not quite sure how the teams = [PlayerList[i :: number_of_teams] line functions, if you could give some clarification it would be much appreciated, thanks. – TechSloth Apr 19 '19 at 04:14
  • I don't understand that error. There are two parameters: `number_of_teams` and `player_names`. – Barmar Apr 19 '19 at 16:27
  • Did you forget to change the `def` line when you copied the code? – Barmar Apr 19 '19 at 16:28
  • I must have, how am I supposed to change it? – TechSloth Apr 24 '19 at 03:50
  • You're supposed to do it like I wrote. Change `def PickTeam(string):` to `def PickTeam(number_of_teams, player_names):` – Barmar Apr 24 '19 at 04:39
  • Like I said, I correctly copied and pasted it directly without changing anything, so I also copied the def line. I repeatedly receive the same error "TypeError: PickTeam() takes 1 positional argument but 2 were given" If need be I can give you a picture of the code Im running and the error via imgur or the like. – TechSloth Apr 26 '19 at 03:57
  • I've added a link to an online demo showing that it works. I don't know what you're doing differently, so post the link. – Barmar Apr 26 '19 at 14:44