【发布时间】:2022-06-15 04:32:39
【问题描述】:
我正在尝试编写一个比赛装置应用程序,该应用程序需要一定数量的球队,让他们相互对抗,而不是连续重复同一支球队两次,即让同一支球队在两天内比赛两次 例如,如果列出的球队是“a”、“b”、“c”、“d”和“e”,并且他们要进行四场比赛,则此代码会打印出类似的结果
a 与 b
c 与 d
c 与 e
但我试图阻止它连续两次重复相同的元素,即
c 与 d
c 与 e
而是
a 与 b
c 与 d
e vs a
代码如下,我需要做哪些修改
import random
number_of_teams = int(input('How many teams? : '))
other_number = number_of_teams + 1
teams = []
number_of_matches = int(input("How many matches are to be played?: "))
real_number_of_matches = number_of_matches + 1
for i in range(1, other_number):
team_name = input("Input team name: ")
teams.append (team_name)
print('The teams participating are', teams)
for i in range(1, real_number_of_matches):
first_team = random.choice(teams)
second_team = random.choice(teams)
if first_team != second_team:
print(f'{first_team} vs {second_team}')```
【问题讨论】:
-
将之前选择的团队保存在一个变量中,并确保它与当前选择的团队之一不同
-
我很确定必须有现有的表来设置比赛中理想的球队对,从而优化两场比赛之间的时间差。它可能不是随机的,而是精心设计的。
标签: python list loops random iteration