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:
watchMinutes = 0
while watchMinutes < 45:
watchMinutes += 1
print(str(watchMinutes) + " 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%.
# You can ignore this import
# line for now, but I'd recommend
# reading the module introduction
# 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)
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!