-3

I'm creating a program in python where i need to ask the user for there team name and team members. I need to have the team names in a list and have the team members embedded within the team name. Any Help?

# Creating Teams

print("Welcome to the program")
print("======================================================")

Teams = []


# Looping to find out the four different team names and players.
for x in range(0, 4) :

    # Asking for the Team name and the team players names
    TeamName = input("What is the name of the team?")
    Player1 = input("What is the first name of the player in your team?")
    Player2 = input("What is the second name of the player in your team?")
    Player3 = input("What is the third name of the player in your team?")
    Player4 = input("What is the last name of the player in your team?")

    Teams.extend([TeamName, Player1, Player2, Player3, Player4])


TeamPlayers = int(input("What Team do you want to see? (0-4)"))

print([Teams, Player1, Player2, Player3, Player4])
Drise
  • 4,310
  • 5
  • 41
  • 66
Flyy
  • 3
  • 2
  • What is the problem/question? Please take the time to read [ask] and the other links found on that page. – wwii Mar 27 '18 at 19:04
  • Any help with *what*, exactly? It appears that your posted code solves the problem you give. – Prune Mar 27 '18 at 19:05
  • 2
    A dictionary might be appropriate here. – user3483203 Mar 27 '18 at 19:05
  • 1
    Maybe use `Teams.append` instead of `Teams.extend` because `extend` will give you one list with all the teams mixed together whereas `append` will give you a list containing a separate list for each team. – Paul Panzer Mar 27 '18 at 19:08
  • Asking for help isn't necessarily asking a question in the context of SO. What problem are you having? Is this just a way to get someone to write code for you? – NonCreature0714 Mar 27 '18 at 19:10
  • i need it not to override the last inputs because i need four teams with each team containing four players – Flyy Mar 27 '18 at 19:13
  • If this has been answered, mark it as so, don't change the title. – Drise Mar 27 '18 at 19:41

3 Answers3

0

I would highly recommend using a dictionary here. Dictionaries allow for easy lookup of keys, and you could easily view the rosters for your teams:

print("Welcome to the program")
print("======================================================")

teams = {}

# Looping to find out the four different team names and players.
for x in range(0, 2) :
    # Asking for the Team name and the team players names
    name = input("What is the name of the team?: ")
    teams[name] = {}
    teams[name]['Player1'] = input("What is the first name of the player in your team?: ")
    teams[name]['Player2'] = input("What is the second name of the player in your team?: ")
    teams[name]['Player3'] = input("What is the third name of the player in your team?: ")
    teams[name]['Player4'] = input("What is the last name of the player in your team?: ")

print(teams)

Output:

Welcome to the program
======================================================
What is the name of the team?: Team1
What is the first name of the player in your team?: Chris
What is the second name of the player in your team?: Chris
What is the third name of the player in your team?: Chris
What is the last name of the player in your team?: Chris
What is the name of the team?: Team2
What is the first name of the player in your team?: Chris
What is the second name of the player in your team?: Chris
What is the third name of the player in your team?: Chris
What is the last name of the player in your team?: Chris
{'Team1': {'Player1': 'Chris',
           'Player2': 'Chris',
           'Player3': 'Chris',
           'Player4': 'Chris'},
 'Team2': {'Player1': 'Chris',
           'Player2': 'Chris',
           'Player3': 'Chris',
           'Player4': 'Chris'}}

If you wanted to view all the players on Team1, you could simply use teams['Team1']

user3483203
  • 50,081
  • 9
  • 65
  • 94
0

(note upper case names are reserved for classes in Python)


A dictionary of dictionaries would make more sense as a data structure here, rather than creating lots of variables only to throw them into lists which loses their keys and just creates more work down the line (unpacking etc.)

So, to do this, you could do something like:

teams = {}
for x in range(0, 4) :
    teamName = input("What is the name of the team?")
    player1  = input("What is the first name of the player in your team?")
    player2  = input("What is the second name of the player in your team?")
    player3  = input("What is the third name of the player in your team?")
    player4  = input("What is the last name of the player in your team?")
    teams[teamName] = {'player_1': player1,
                       'player_2': player2,
                       'player_3': player3,
                       'player_4': player4}

which gives teams as a readable and accessible structure:

{'team one': {'player_4': 'james', 'player_3': 'jim', 'player_1': 'bob', 'player_2': 'bill'},
 'team two': {'player_4': 'cat', 'player_3': 'fish', 'player_1': 'jake', 'player_2': 'paul'},
 'team three': {'player_4': 'sharpener', 'player_3': 'ruler', 'player_1': 'table', 'player_2': 'paper'},
 'team four': {'player_4': 'pencil', 'player_3': 'pen', 'player_1': 'shoe', 'player_2': 'book'}
}

So, now, you could access with something like: teams['team one']['player_1'].


Or you could use the same idea, but use a list of dictionaries (and include the team name as a val in the dictionary for each team).

teams = []
for x in range(0, 4) :
    teamName = input("What is the name of the team?")
    player1  = input("What is the first name of the player in your team?")
    player2  = input("What is the second name of the player in your team?")
    player3  = input("What is the third name of the player in your team?")
    player4  = input("What is the last name of the player in your team?")
    teams.append({'player_1': player1,
                  'player_2': player2,
                  'player_3': player3,
                  'player_4': player4})
Joe Iddon
  • 20,101
  • 7
  • 33
  • 54
  • @Flyy Please accept the answer that worked (grey --> green tick next to answer) to show appreciation... thank you :) – Joe Iddon Mar 27 '18 at 20:16
0

I'm going to assume for a minute that you're going to need to input more than 1 team. For this application, you're better off going with a dictionary of lists:

Teams = {}
players = 2
no_of_teams = 2
for n in range(no_of_teams):
    TeamName = input("What is the name of the team?")
    Teams[TeamName] = []
    for x in range(players):
        # Asking for the Team name and the team players names
        player = input("Please enter the name of player #%s in your team:" % (x+1))
        Teams[TeamName].append(player)

[print(team, players) for team, players in Teams.items()]

or a dictionary of dictionaries if you care about the assigning a player number to each name:

Teams = {}
players = 2
no_of_teams = 2
for n in range(no_of_teams):
    TeamName = input("What is the name of the team?")
    Teams[TeamName] = {}
    for x in range(players):
        player = input("Please enter the name of player #%s in your team:" % (x+1))
        Teams[TeamName]['player%s' % (x+1)] = player

[print(team, players) for team, players in Teams.items()]
Alaaedeen
  • 390
  • 3
  • 7