简体   繁体   中英

Creating lists from list

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()

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM