Why aren't both consecutive for loops executed?

529 Views Asked by At

[Beginner alert] I'm writing codes and taking notes about their variations. I wanted to combine the variations of this for loop in a single file, yet only the first one is executed. Why and what do i have to do in order to achieve that?

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
1

There are 1 best solutions below

0
On

The second part assumes that the value of n hasn't changed since the end of the first part, causing the execution not printing any results. [Hinted by Mark Meyer.]