0

Forgive me if this question is a repeat, but I'm stuck with a code I've been working on. I am creating a program for myself to create random teams of people, and I've been trying to find an easy way to make an inserted amount of teams. The area I am suck on is here:

print("How many teams would you like?")
numberteam = input("Number: ")

listnumber = 0

teams = []
while numberteam + 1:
    teams.append(Team(str(listnumber + 1)) = [])

I am a fairly new coder, so I'm sure besides the obvious using an expression for a variable there are probably other mistakes, but any suggestions on an easy way to fix this would be great!

Also if I left something out, please just ask! (By the way I am using python)

10
  • 2
    I'm having a hard time understanding your code, but it seems like a combination of random.shuffle and list slicing would be your best bet... Commented Oct 7, 2013 at 1:57
  • 1
    Also, keep in mind that python isn't C. while numberteam + 1: isn't doing what you expect. It is adding 1 to numberteam, then discards the value. numberteam is not being updated. Commented Oct 7, 2013 at 1:58
  • So seth, I was hoping to create new lists, and have their variables be Team1, Team2... etc. until the desired amount of teams was reached. Then, later on, I would take the names of people, randomly shuffle them, and put them into these lists. Commented Oct 7, 2013 at 2:00
  • 1
    I see, you want a dictionary! Commented Oct 7, 2013 at 2:01
  • Oh yes I haven't coded in awhile, I know what I can do to fix that. Im basically just wondering how I would be able to make a new list for every iteration, to create the desired amount of teams. Commented Oct 7, 2013 at 2:02

2 Answers 2

3

Try this (I cleaned things up a bit):

print("How many teams would you like?")
numberteam = int(input("Number: "))
# Create a dictionary, where each team has an emtpy list
teams = dict([('Team{}'.format(i), []) for i in range(numberteam)])
# The following also works
# teams = {'Team{}'.format(i):[] for i in range(numberteam)}

You can then access the teams like this:

teams['Team3'] # returns an empty list

The longhand for the above is

print("How many teams would you like?")
numberteam = int(input("Number: "))
teams = {}
for i in range(numberteam):
    teams['Team{}'.format(i)] = []
Sign up to request clarification or add additional context in comments.

7 Comments

Yes, this is exactly what I was looking for! I am a new coder, and haven't quite gotten around to looking into what dictionary does. Thanks!
Yes, I wasn't sure if the OP had python2 or python3, so I went with a solution that would work for both. Although, now that I think about it it looks like python3 based on the print syntax and input.
@SethMMorton Check the tags ;)
@Haidro Haha, I'm really unobservant tonight
@SethMMortonn I am sorry to bring this back, but I have another question. Now that I have these empty team lists, how do I randomly place people in these lists, according to how many teams the user wants?
|
1

Maybe you are looking for something like this:

import random

persons = ['Adan', 'Boris', 'Carla', 'Dora', 'Ernesto', 'Floridalma', 'Gustavo', 'Hugo', 'Ines', 'Juan']
random.shuffle (persons)

count = int(input('How many teams? '))
teams = [persons[i::count] for i in range(count)]
for idx, team in enumerate(teams):
    print ('Team {}: {}'.format(idx + 1, ', '.join(team)))

The content of person doesn't need to be strings, it can hold instances of a custom Person class or whatever you like.


Explanation:

  1. Populate a list from which to chose.

  2. Shuffle it in place.

  3. Prompt the user for the number of teams and convert the input into an integer.

  4. Create the teams. person[i::count] picks from persons each count-th element starting at index i. Hence if e.g. count is 3, in the first team are the (shuffled) indices 0, 3, 6, etc, in the second team 1, 4, 7, etc, etc.

  5. Print it.


The general slicing notation is iterable[start:stop:step].


Or if you want to use dictionaries, you can use a dictionary comprehension:

teams = {'Team{}'.format(i + 1): persons[i::count] for i in range(count)}
for k, v in teams.items():
    print ('{}: {}'.format(k, ', '.join(v) ) )

3 Comments

I see... it looked like C++'s namespace syntax at first. I should have read that at a slice.
Thanks to python's list comprehensions and slicing, you can build your teams by just executing teams = [persons[i::count] for i in range(count)].
@SethMMorton I added a second version using dictionaries instead of lists.

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.