# Loops

## While Loops in Python

As seen with their ‘for’ counterpart, loops are a really useful tool that allow us to repeat instructions without repeating our code. It keeps programs light and easier to understand. For loops ran for a set amount of times – a number that we explicitly provide. Where while loops differ is that they will run until told not to. Take the example below:

In [1]:
```watchMinutes = 0

while watchMinutes < 45:
watchMinutes += 1

print(str(watchMinutes) + " played, it is half-time!")
```
```45 played, it is half-time!
```

Above, the referee starts their watch at 0 minutes. A while loop then begins, increasing the watchMinutes counter by 1 each time it runs.

It runs until the statement after ‘while’ becomes false (in this case, until minutes passes 45). It then stops and continues with the rest of the code – telling us that it is half time.

The structure is pretty simple, but we have to be very careful when using while loops. If the while statement never turns to ‘False’, we will create an infinite loop and break the program. Use carefully, or just use for loops where you can!

Let’s get another, more complex example, for a team that wants to see how many passes it might make, depending on its pass success rate.

It wants to know how many passes it can expect to make in a row, if they know that their pass success rate is 66%.

In [2]:
```# You can ignore this import
# line for now, but I'd recommend
# very soon - why not now?!
import random

keepPossession = True
passSkill = 66
passesComplete = 0

while keepPossession == True:

if random.randint(0,100) > passSkill:
keepPossession = False
else:
passesComplete += 1

print(passesComplete)
```
```1
```

So if your team has a 66% pass completion rate, you really can’t expect much flowing football!

Let’s break this down.

After importing the ‘random’ module, we define a few things:
keepPossession: this tells Python is it ‘True’ or ‘False’ that we keep the ball?
passSkill: The % chance that we will keep the ball after a pass. Feel free to change this and see what numbers come up (but avoid 100 or more!)!
passesComplete: This is simply our counter of how many passes.

We then run our while loop. This will run infinitely until we lose possession (keepPossession equals False).

In this loop, we add one pass to our counter, then run a random number generator. If this random number is great than our skill level, we lose the ball and set keepPossession to False.

Once the loop is over, we receive our passesComplete count. If we do this lots of times, we’ll get a good average that we can expect to make each time, with some distribution above and below this number. Pretty cool – maybe find the % for your team and see what you can expect!

## Summary

While loops give us another way to repeat our code, similar to the for loop. The structure is just as simple, but we need to be careful not to send our program into an infinite loop and completely break it!

We used these loops to act as a ref’s watch and to estimate how many passes a team might string together on any given opportunity. You’re now adding numbers to your scouting report & have something to train your team against too!

Next up, read more about the random module or modules in general!

## For Loops

Everything that we have seen so far has involved us giving a program a single instruction and asking it to run. This is not particularly efficient if we want to do the same thing several times over. We would much rather find a way to automate this and run the task multiple times for us, for loops are the perfect way to get started here.

#### Lists

We cover lists and other group concepts elsewhere, but a list is simply a way to group lots of things into one variable. A list has lots of functions that make it easy to work with – please see its own page for more on this. This list is a 3-a-side team selected to play in a TV commercial.

In [1]:
```TeamA = ["Nasta","Overvenus","Ranalda"]
```

The TV commercial wants to announce these players to the crowd. We can do this individually for each:

In [2]:
```print("Welcome to the cage, " + TeamA[0])
print("Welcome to the cage, " + TeamA[1])
print("Welcome to the cage, " + TeamA[2])
```
```Welcome to the cage, Nasta
Welcome to the cage, Overvenus
Welcome to the cage, Ranalda
```

Notice how we repeated ourself three times? ‘For loops’ help us to avoid doing so. The smarter announcer is run using the following code:

In [3]:
```for player in TeamA:
print("Welcome to the cage, " + player)
```
```Welcome to the cage, Nasta
Welcome to the cage, Overvenus
Welcome to the cage, Ranalda
```

Our output is exactly the same, but the code looks so much better!

The essential bits to our code are the words ‘for’ and ‘in’, ***

Plus, we could also add a second team really really easily with a ‘nested for-loop’ – this is a for-loop inside a for-loop!

In [4]:
```#Team 2, enter!
TeamB = ["Tatti","Nikita","Hanry"]

#A list of lists!
Teams = [TeamA,TeamB]

for team in Teams:
for player in team:
print("Welcome to the cage, " + player)
```
```Welcome to the cage, Nasta
Welcome to the cage, Overvenus
Welcome to the cage, Ranalda
Welcome to the cage, Tatti
Welcome to the cage, Nikita
Welcome to the cage, Hanry
```

So cool!

### Summary

If we have to write down every single thing that we want our code to do, it will be hugely inefficient and a bit of a pain to write. We can utilise for loops to automate any repetition and to try our best to avoid duplicate code. In fact, programming has a DRY mantra – “Don’t repeat yourself”.

Our examples above demonstrate a nested for-loop too – a loop within a loop to push our automation even further.

Give them a try yourself, and check out while loops when you’re comfortable here!