-2

Currently I'm working on a personal project... Where I want to randomize teams depending on the amount of people that are going to join. I'm creating a list where I'm adding the users into. And counting the amount of users that are joining. After I need to add users towards new lists. Is there a way to do that in the function groups? Or should I look at it from another direction?

def participents():
    users = []
    counter = 0
    while True:
        participent = input('Who is gonna join? ')
        if participent != 'stop':
            users.append(participent)
        elif participent == 'stop':
            break
    for user in users:
        counter +=1

    def groups():
        if counter % 3 == 0:
            ListsNeeded = counter / 3
            IntListsNeeded = int(ListsNeeded)
            print(IntListsNeeded)
            print("It's gonna be trio's")

        else:
            ListsNeeded = counter / 2
            IntListsNeeded = int(ListsNeeded)
            print(IntListsNeeded)
            print("It's gonna be duo's")


    groups()

participents()
Patrick Artner
  • 50,409
  • 9
  • 43
  • 69
Eric
  • 23
  • 1
  • 9
  • You can use `random.choices` from the standard library. – Axe319 Oct 21 '21 at 14:57
  • 3
    `for user in users: counter += 1` ? why? `counter == len(users)` ... `elif participent == 'stop': break` ? why? `else: break` Use groups() does noting with the users ... you may want to look into `random.shuffle(users)` to randomize the participents in the list and look into [how-do-you-split-a-list-into-evenly-sized-chunks](https://stackoverflow.com/questions/312443/how-do-you-split-a-list-into-evenly-sized-chunks) for splitting it ... searching SO will in 95% of all beginner cases provide enough input so you can solve it yourself ... so no need to ask at all – Patrick Artner Oct 21 '21 at 14:57
  • If you have a specific problem make your code shorter : if you are clear about how to ask participents and put them into a list - why bloat your [mre] with 10 lines doing what you know how to do? Simply hardcode a list: `users_case_1 = ["a","b","c","d","e"]` (ie % 3 == 2) and `users_case_2 = ["a","b","c","d","e","f"]` (ie % 3 == 0) and state your real problem more clearly - this is not a discussion forum so open ended "how to do it better" questions often are ill regarded as they tend to show not much research effort. What exactly IS your problem btw? – Patrick Artner Oct 21 '21 at 15:03
  • Btw. randomizing a list has also hundreds of posts, f.e. [best-way-to-randomize-a-list-of-strings-in-python](https://stackoverflow.com/questions/1022141/best-way-to-randomize-a-list-of-strings-in-python) - so essentially both your to-dos (randomizing, partitioning) are solved problems... – Patrick Artner Oct 21 '21 at 15:09

1 Answers1

0

One suggestion I would make is to replace this:

for user in users:
    counter +=1

with just:

counter = len(users)

And assuming you're on Python 3.8+, another suggestion would be to replace this:

users = []
while True:
    participent = input('Who is gonna join? ')
    if participent != 'stop':
        users.append(participent)
    elif participent == 'stop':
        break

with this:

users = []
while participent := input('Who is gonna join? ') != 'stop':
    users.append(participent)
rv.kvetch
  • 9,940
  • 3
  • 24
  • 53