-1

[初学者提醒] 我正在编写代码并记录它们的变化。我想将这个 for 循环的变体组合在一个文件中,但只执行第一个。为什么以及我必须做什么才能实现这一目标?

teams = ['Dragons', 'Wolves', 'Pandas', 'Unicorns']
n = 1

for home_team in teams:
    for away_team in teams[n:]:     # This block causes the execution to increase n by one
        if home_team != away_team:  # for every away_team in one home_team
            print(home_team, away_team)  # then proceeds to the next home_team
            n += 1

for home_team in teams:
    for away_team in teams[n:]:     # This block causes the execution to increase n by one
        if home_team != away_team:  # for every home_team
            print(home_team, away_team)
    n += 1
4

1 回答 1

0

第二部分假设 n 的值自第一部分结束以来没有改变,导致执行不打印任何结果。[由马克迈耶暗示。]

于 2020-07-19T16:14:31.233 回答