Skip to main content
added 384 characters in body
Source Link
toolic
  • 16.4k
  • 6
  • 29
  • 221

DRY

The code for picking the 2 teams is mostly duplicated. You could move some of the code into a function, and call the function once for each team.

For example, to input the team captain, here is the function:

def get_captain(team):
    """
    Ask user to input a team captain name for a specified team number.
    Input: team is an integer
    Returns a string
    """
    captain = input(f"Hello Team {team}\nChoose your captain: ")
    while captain == "":
        captain = input(f"Team {team} Captain Needed!: ")
    return captain

and here is how to call it:

# Team 1 selection
if team_1:
    if len(team_1) < 8:
        pick = random.choice(available_players)
        team_1.append(pick)
        available_players.remove(pick)
else:
    team_1.append(get_captain(1))

For Team 2, use:

    team_2.append(get_captain(2))

You may be able to eliminate the duplication with the pick code as well. Alternately, you could eliminate the multiple calls to choice if you shuffle the array before the while loop:

random.shuffle(available_players)

# Loop until both teams have 8 players
while len(team_1) < 8 and len(team_2) < 8:

    # Team 1 selection
    if team_1:
        if len(team_1) < 8:
            team_1.append(available_players.pop())
    else:

Documentation

The PEP 8 style guide recommends adding docstrings for functions. The function above shows an example docstring.

You could also add a docstring at the top of the code to summarize its purpose.

Naming

x is not a meaningful variable name in this context. As shown above, captain is a more meaningful name.

Input

Consider using pyinputplus for input. It has standard ways of checking for valid input.

Magic number

The number 8 shows up several times. You could create a constant instead:

TEAM_SIZE = 8

if len(team_1) < TEAM_SIZE:

DRY

The code for picking the 2 teams is mostly duplicated. You could move some of the code into a function, and call the function once for each team.

For example, to input the team captain, here is the function:

def get_captain(team):
    """
    Ask user to input a team captain name for a specified team number.
    Input: team is an integer
    Returns a string
    """
    captain = input(f"Hello Team {team}\nChoose your captain: ")
    while captain == "":
        captain = input(f"Team {team} Captain Needed!: ")
    return captain

and here is how to call it:

# Team 1 selection
if team_1:
    if len(team_1) < 8:
        pick = random.choice(available_players)
        team_1.append(pick)
        available_players.remove(pick)
else:
    team_1.append(get_captain(1))

For Team 2, use:

    team_2.append(get_captain(2))

You may be able to eliminate the duplication with the pick code as well.

Documentation

The PEP 8 style guide recommends adding docstrings for functions. The function above shows an example docstring.

You could also add a docstring at the top of the code to summarize its purpose.

Naming

x is not a meaningful variable name in this context. As shown above, captain is a more meaningful name.

Input

Consider using pyinputplus for input. It has standard ways of checking for valid input.

Magic number

The number 8 shows up several times. You could create a constant instead:

TEAM_SIZE = 8

if len(team_1) < TEAM_SIZE:

DRY

The code for picking the 2 teams is mostly duplicated. You could move some of the code into a function, and call the function once for each team.

For example, to input the team captain, here is the function:

def get_captain(team):
    """
    Ask user to input a team captain name for a specified team number.
    Input: team is an integer
    Returns a string
    """
    captain = input(f"Hello Team {team}\nChoose your captain: ")
    while captain == "":
        captain = input(f"Team {team} Captain Needed!: ")
    return captain

and here is how to call it:

# Team 1 selection
if team_1:
    if len(team_1) < 8:
        pick = random.choice(available_players)
        team_1.append(pick)
        available_players.remove(pick)
else:
    team_1.append(get_captain(1))

For Team 2, use:

    team_2.append(get_captain(2))

You may be able to eliminate the duplication with the pick code as well. Alternately, you could eliminate the multiple calls to choice if you shuffle the array before the while loop:

random.shuffle(available_players)

# Loop until both teams have 8 players
while len(team_1) < 8 and len(team_2) < 8:

    # Team 1 selection
    if team_1:
        if len(team_1) < 8:
            team_1.append(available_players.pop())
    else:

Documentation

The PEP 8 style guide recommends adding docstrings for functions. The function above shows an example docstring.

You could also add a docstring at the top of the code to summarize its purpose.

Naming

x is not a meaningful variable name in this context. As shown above, captain is a more meaningful name.

Input

Consider using pyinputplus for input. It has standard ways of checking for valid input.

Magic number

The number 8 shows up several times. You could create a constant instead:

TEAM_SIZE = 8

if len(team_1) < TEAM_SIZE:
added 154 characters in body
Source Link
toolic
  • 16.4k
  • 6
  • 29
  • 221

DRY

The code for picking the 2 teams is mostly duplicated. You could move some of the code into a function, and call the function once for each team.

For example, to input the team captain, here is the function:

def get_captain(team):
    """
    Ask user to input a team captain name for a specified team number.
    Input: team is an integer
    Returns a string
    """
    captain = input(f"Hello Team {team}\nChoose your captain: ")
    while captain == "":
        captain = input(f"Team {team} Captain Needed!: ")
    return captain

and here is how to call it:

# Team 1 selection
if team_1:
    if len(team_1) < 8:
        pick = random.choice(available_players)
        team_1.append(pick)
        available_players.remove(pick)
else:
    team_1.append(get_captain(1))

For Team 2, use:

    team_2.append(get_captain(2))

You may be able to eliminate the duplication with the pick code as well.

Documentation

The PEP 8 style guide recommends adding docstrings for functions. The function above shows an example docstring.

You could also add a docstring at the top of the code to summarize its purpose.

Naming

x is not a meaningful variable name in this context. As shown above, captain is a more meaningful name.

Input

Consider using pyinputplus for input. It has standard ways of checking for valid input.

Magic number

The number 8 shows up several times. You could create a constant instead:

TEAM_SIZE = 8

if len(team_1) < TEAM_SIZE:

DRY

The code for picking the 2 teams is mostly duplicated. You could move some of the code into a function, and call the function once for each team.

For example, to input the team captain, here is the function:

def get_captain(team):
    """
    Ask user to input a team captain name for a specified team number.
    Input: team is an integer
    Returns a string
    """
    captain = input(f"Hello Team {team}\nChoose your captain: ")
    while captain == "":
        captain = input(f"Team {team} Captain Needed!: ")
    return captain

and here is how to call it:

# Team 1 selection
if team_1:
    if len(team_1) < 8:
        pick = random.choice(available_players)
        team_1.append(pick)
        available_players.remove(pick)
else:
    team_1.append(get_captain(1))

For Team 2, use:

    team_2.append(get_captain(2))

You may be able to eliminate the duplication with the pick code as well.

Documentation

The PEP 8 style guide recommends adding docstrings for functions. The function above shows an example docstring.

You could also add a docstring at the top of the code to summarize its purpose.

Naming

x is not a meaningful variable name in this context. As shown above, captain is a more meaningful name.

Input

Consider using pyinputplus for input. It has standard ways of checking for valid input.

DRY

The code for picking the 2 teams is mostly duplicated. You could move some of the code into a function, and call the function once for each team.

For example, to input the team captain, here is the function:

def get_captain(team):
    """
    Ask user to input a team captain name for a specified team number.
    Input: team is an integer
    Returns a string
    """
    captain = input(f"Hello Team {team}\nChoose your captain: ")
    while captain == "":
        captain = input(f"Team {team} Captain Needed!: ")
    return captain

and here is how to call it:

# Team 1 selection
if team_1:
    if len(team_1) < 8:
        pick = random.choice(available_players)
        team_1.append(pick)
        available_players.remove(pick)
else:
    team_1.append(get_captain(1))

For Team 2, use:

    team_2.append(get_captain(2))

You may be able to eliminate the duplication with the pick code as well.

Documentation

The PEP 8 style guide recommends adding docstrings for functions. The function above shows an example docstring.

You could also add a docstring at the top of the code to summarize its purpose.

Naming

x is not a meaningful variable name in this context. As shown above, captain is a more meaningful name.

Input

Consider using pyinputplus for input. It has standard ways of checking for valid input.

Magic number

The number 8 shows up several times. You could create a constant instead:

TEAM_SIZE = 8

if len(team_1) < TEAM_SIZE:
added 83 characters in body
Source Link
toolic
  • 16.4k
  • 6
  • 29
  • 221

DRY

The code for picking the 2 teams is mostly duplicated. You could move some of the code into a function, and call the function once for each team.

For example, to input the team captain, here is the function:

def get_captain(team):
    """
    Ask user to input a team captain name for a specified team number.
    Input: team is an integer
    Returns a string
    """
    captain = input(f"Hello Team {team}\nChoose your captain: ")
    while captain == "":
        captain = input(f"Team {team} Captain Needed!: ")
    return captain

and here is how to call it:

# Team 1 selection
if team_1:
    if len(team_1) < 8:
        pick = random.choice(available_players)
        team_1.append(pick)
        available_players.remove(pick)
else:
    team_1.append(get_captain(1))

For Team 2, use:

    team_2.append(get_captain(2))

You may be able to eliminate the duplication with the pick code as well.

Documentation

The PEP 8 style guide recommends adding docstrings for functions. The function above shows an example docstring.

You could also add a docstring at the top of the code to summarize its purpose.

Naming

x is not a meaningful variable name in this context. As shown above, captain is a more meaningful name.

Input

Consider using pyinputplus for input. It has standard ways of checking for valid input.

DRY

The code for picking the 2 teams is mostly duplicated. You could move some of the code into a function, and call the function once for each team.

For example, to input the team captain, here is the function:

def get_captain(team):
    """
    Ask user to input a team captain name for a specified team number.
    Input: team is an integer
    Returns a string
    """
    captain = input(f"Hello Team {team}\nChoose your captain: ")
    while captain == "":
        captain = input(f"Team {team} Captain Needed!: ")
    return captain

and here is how to call it:

# Team 1 selection
if team_1:
    if len(team_1) < 8:
        pick = random.choice(available_players)
        team_1.append(pick)
        available_players.remove(pick)
else:
    team_1.append(get_captain(1))

For Team 2, use:

    team_2.append(get_captain(2))

You may be able to eliminate the duplication with the pick code as well.

Documentation

The PEP 8 style guide recommends adding docstrings for functions. The function above shows an example docstring.

Naming

x is not a meaningful variable name in this context. As shown above, captain is a more meaningful name.

Input

Consider using pyinputplus for input. It has standard ways of checking for valid input.

DRY

The code for picking the 2 teams is mostly duplicated. You could move some of the code into a function, and call the function once for each team.

For example, to input the team captain, here is the function:

def get_captain(team):
    """
    Ask user to input a team captain name for a specified team number.
    Input: team is an integer
    Returns a string
    """
    captain = input(f"Hello Team {team}\nChoose your captain: ")
    while captain == "":
        captain = input(f"Team {team} Captain Needed!: ")
    return captain

and here is how to call it:

# Team 1 selection
if team_1:
    if len(team_1) < 8:
        pick = random.choice(available_players)
        team_1.append(pick)
        available_players.remove(pick)
else:
    team_1.append(get_captain(1))

For Team 2, use:

    team_2.append(get_captain(2))

You may be able to eliminate the duplication with the pick code as well.

Documentation

The PEP 8 style guide recommends adding docstrings for functions. The function above shows an example docstring.

You could also add a docstring at the top of the code to summarize its purpose.

Naming

x is not a meaningful variable name in this context. As shown above, captain is a more meaningful name.

Input

Consider using pyinputplus for input. It has standard ways of checking for valid input.

Source Link
toolic
  • 16.4k
  • 6
  • 29
  • 221
Loading