I want to make a program that randomly simulates the flow of a champions league tournament (football/soccer tournament). The format of the tournament is as follows:
- There are 32 teams that qualify each year.
- The teams are then randomly distributed into 8 groups containing 4 teams each.
- The teams all play each other and 2 of the best performing ones qualify to the next round (round of sixteen)
- The matchups are decided by randomly selecting the teams 2 at a time with the constraint that no two teams from the same group may face each other again
- The winner then progresses to the next stage and so on and so forth until we have a winner.
In my program I have tried to implement this format using the random module in python. However, once every 4-5 iterations I get an index error at either the round of 16 selection step or the quarter final selection step. I cant seem to understand why. Please help
import random
teams = ["Barcelona", "Atlético Madrid", "Real Madrid", "Valencia", "Manchester City", "Liverpool", "Chelsea", "Tottenham Hotspur", "Juventus", "Napoli", "Atalanta", "Internazionale Milano", "Bayern München", "Borussia Dortmund", "RB Leipzig", "Bayer Leverkusen", "Paris Saint-Germain", "LOSC Lille", "Lyon", "Zenit", "Lokomotiv Moskva", "Benfica", "Shakhtar Donetsk", "Genk", "Club Brugge", "Galatasaray", "Salzburg", "Slavia Praha", "Ajax", "Olympiacos", "GNK Dinamo", "Crvena zvezda"]
random.shuffle(teams) #shuffling the 32 teams
Group_A = [] #making empty lists of all 8 groups
Group_B = []
Group_C = []
Group_D = []
Group_E = []
Group_F = []
Group_G = []
Group_H = []
for i in range(32): #using for loop to add each of the 32 teams in group lists
if i <=3:
Group_A.append(teams[i])
elif i <=7 and i>3:
Group_B.append(teams[i])
elif i <=11 and i>7:
Group_C.append(teams[i])
elif i <=15 and i>11:
Group_D.append(teams[i])
elif i <=19 and i>15:
Group_E.append(teams[i])
elif i <=23 and i>19:
Group_F.append(teams[i])
elif i <=27 and i>23:
Group_G.append(teams[i])
else:
Group_H.append(teams[i])
GroupDict = [Group_A, Group_B, Group_C, Group_D, Group_E, Group_F, Group_G, Group_H]
#adding all group lists to another list
print("Group A:\n", Group_A) #displaying group stage draws
print("Group B:\n", Group_B)
print("Group C:\n", Group_C)
print("Group D:\n", Group_D)
print("Group E:\n", Group_E)
print("Group F:\n", Group_F)
print("Group G:\n", Group_G)
print("Group H:\n", Group_H)
r16 = [] #making empty list for the round of sixteen
for i in range(8): #randomly selecting 2 winners from each group and adding
list = GroupDict[i] #and adding them to r16 list 2 at a time
n = 2
r16.append(random.sample(list, n))
print("\n\nTeams that qualified to the round of 16:",r16)
r16_matches = [] #making an empty list to store randomised matchups
for i in range(16): #code to select 2 random teams with
t = random.choice(r16) #the constraint that no two teams from
if not t: #the same group may face each other again
continue
else:
t1 = random.choice(t)
t.remove(t1)
while (1==1):
s = random.choice(r16)
if not s:
continue
else:
if r16.index(s) != r16.index(t):
t2 = random.choice(s)
s.remove(t2)
break
else:
continue
match = [t1,t2]
r16_matches.append(match)
print("\n\nRound of 16 matchups:", r16_matches, "\n\n")
Qfinals = []
Even = [0,2,4,6]
for j in Even: #selecting one winner from each round of sixteen matchup and putting them together, 2 at a time
qt1 = random.choice(r16_matches[j])
qt2 = random.choice(r16_matches[j+1])
qm = [qt1, qt2]
Qfinals.append(qm)
for l in range(4):
print("Quarter final ",l+1," : ",Qfinals[l][0], " vs ",Qfinals[l][1])
Sfinals = []
for k in range(4): #selecting a team from each quarter final matchup to face in semifinals, 2 at a time
S = random.choice(Qfinals[k])
Sfinals.append(S)
print("\n\nSemi Final teams:", Sfinals)
ft1l = [Sfinals[0], Sfinals[1]]
ft2l = [Sfinals[2], Sfinals[3]]
finals = [random.choice(ft1l), random.choice(ft2l)] #selecting random winner from each semi final
print("\n\nFinals: ", finals) #printing finals
print("\n\nWinner: ", random.choice(finals)) #randomly selecting winner from final
Aucun commentaire:
Enregistrer un commentaire