我想制作一个随机模拟冠军联赛(足球/足球比赛)流程的程序。比赛形式如下:
在我的程序中,我尝试使用 python 中的 random 模块来实现这种格式。但是,每 4-5 次迭代,我就会在 16 轮选择步骤或四分之一决赛选择步骤中出现索引错误。我似乎无法理解为什么。请帮忙
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
答案 0 :(得分:1)
到达线路时:
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)
有时列表 r16_matches
仅包含 7 个(索引 0-6)而不是 8 个条目(索引 0-7)。当 j 为 6 时,这会导致 r16_matches[j+1]
引发索引错误。
我认为,你需要找到这个错误的根源,才能解决你的索引错误。通常在这种情况下,将代码分解成更小的部分会很有帮助,例如通过使用函数。然后,您可以逐个调试函数并验证它们是否完全符合您的期望。
答案 1 :(得分:1)
顺便说一句,我发现了错误。我会尽力解释。 for 循环正在处理的列表 r16 的形式为:[[1,2],[3,4],[5,6],[7,8]](由 2 个子列表组成的列表)。 for 循环然后将它们分为两个一组,约束条件是没有两个元素可能来自 r16 中的同一个子列表。所以 [3,5],[7,1] 等是有效的配对,但 [1,2],[4,3] 等不是(来自 r16 中相同子列表的元素)。
现在,如果您注意到循环是否生成以下对:[1,3],[2,6],[4,5]。这些是完全有效的对,但只剩下 [7,8] 不能配对。这会混淆块,因此,它无法填充 r16_matches 列表中的所有对。所以我添加了一段代码来处理这个问题,现在代码完全没有错误。感谢大家的帮助! :)