make a function that makes a world cup draw in python

To make a World Cup draw in Python, we can follow these steps:

  1. Create a list of participating countries.
  2. Shuffle the list randomly.
  3. Divide the list into 8 groups with 4 teams each.
  4. Print the results of each group.

Here's an example code that implements this algorithm as a function:

main.py
import random

def make_world_cup_draw(countries):
    random.shuffle(countries)  # shuffle participating countries randomly
    groups = [[] for i in range(8)]  # create 8 empty groups
    for i, country in enumerate(countries):
        group_index = i % 8  # distribute the countries evenly into 8 groups
        groups[group_index].append(country)
    for i, group in enumerate(groups):
        print(f"Group {chr(i+65)}:")  # convert index to alphabetic character (A to H)
        for country in group:
            print(f"- {country}")  # print each team in the group
        print()  # empty line for spacing

# Example usage:
participating_countries = ["Russia", "Germany", "Brazil", "Portugal", "Argentina", "Belgium", "Poland", "France",
                           "Spain", "Peru", "Switzerland", "England", "Colombia", "Mexico", "Uruguay", "Croatia"]
make_world_cup_draw(participating_countries)
906 chars
19 lines

This function takes a list of participating countries as an argument, shuffles the list using the random.shuffle() function, divides the list into 8 groups of 4 teams each, and prints the results of each group in alphabetical order (from Group A to Group H).

gistlibby LogSnag