-1

Hello I hope you can help me,

I'm trying to make an algorithm that takes a txt file with a list of names, reorder them randomly and assign them to teams.

It works, the only issue I have is that for some reason it adds an extra space in all rows of the list, except the first element. It is not the txt file, the list is fine there.

for example, I want an output like this:

team 1
dude1
dude2
team 2
dude3
dude4

Instead it does this:

team 1
 dude1
 dude2
 team 2
 dude3
 dude4

so team 1 does it without the space but everything else has a space at the beggining.

Here's the code:

import random

raw_list = open("list.txt","r")

people_list = []
for line in raw_list.readlines():
    people_list.append(line)
    
raw_list.close()

team_list = []

team_num = int(input("enter the number of teams you want "))

len_people = int(len(people_list))
teams = len_people / team_num
len_people=int(len_people+team_num)

j = 1
i = 1
for n in range(len_people):
    if i == 1:
        if j>team_num:
            popper = random.randint(0, (len(people_list)) - 1)
            team_list.append(people_list.pop(popper))
            
        else:
            team_list.append(f"Team {j}\n")
            i = i + 1
            
    elif i < teams:
        i=i+1
        popper = random.randint(0, (len(people_list)) - 1)
        team_list.append(people_list.pop(popper))
        
    else:
        i = 1
        j = j + 1
        popper = random.randint(0, (len(people_list)) - 1)
        team_list.append(people_list.pop(popper))
           
    
print (*team_list)

thanks

  • Please supply the expected [minimal, reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). Show where the intermediate results differ from what you expected. We should be able to copy and paste a contiguous block of your code, execute that file, and reproduce your problem along with tracing output for the problem points. This lets us test our suggestions against your test data and desired output. – Prune Dec 02 '20 at 22:30
  • Posting your entire program is excessive. Format your output descriptions to show the error. Your current code fails because we don't have your input file. Replace that input by simply hard-coding your test data within your posted example. – Prune Dec 02 '20 at 22:32
  • Are you sure you created you example with your code? The example contains "team" in lower-case but the code in upper-case. – bjhend Dec 02 '20 at 23:07

3 Answers3

0

print(*team_list) is like passing each element of team_list as positional arguments. By default, print puts a space between each argument. Change it to print(*team_list, sep='') to suppress the default space separator.

ShadowRanger
  • 143,180
  • 12
  • 188
  • 271
0

When you print multiple value in a single call to print(), it puts a space between them. E.g. if you write

a = "abc"
b = "def"
print(a, b)

it prints

abc def

So when you call print(*team_list) it puts a space between each team member.

You can use the sep argument to change this:

print(team_list, sep="")
Barmar
  • 741,623
  • 53
  • 500
  • 612
0

Formatting will be easier if you remove the newlines from the names using strip when you read them out of the file. In general life is easier if you keep your data separate from the way you want to display it; it's easy to add formatting in later, but hard to remove/modify it when it's all mixed into everything.

import random

# Get complete list of people.
with open("list.txt", "r") as raw_list:
    people_list = [line.strip() for line in raw_list.readlines()]

# Build list of (empty) teams.
team_list = [[] for _ in range(int(
    input("enter the number of teams you want ")
))]

# Shuffle people and assign them to each team in turn.
random.shuffle(people_list)
for i, person in enumerate(people_list):
    team_list[i % len(team_list)].append(person)

# Output.
for i, team in enumerate(team_list):
    print(f"Team {i+1}")
    for person in team:
        print(person)

prints:

enter the number of teams you want 2
Team 1
dude2
dude3
Team 2
dude4
dude1

Each team containing simply a list of names with no extra newlines etc means that if you decide you want to change the way you display the teams, it's very simple:

for i, team in enumerate(team_list):
    print(f"Team {i+1}:", ", ".join(team))
enter the number of teams you want 2
Team 1: dude2, dude3
Team 2: dude1, dude4
Samwise
  • 68,105
  • 3
  • 30
  • 44