Forgive me if it seems like I'm repeating myself. But we are going to look at loops, again.
This time it a different type of loop, the for loop. And while we are tackling it, we will learn a bit more about data in Python.
last tutorial. The for loop let's us repeat code a fixed number of times.
We are going to modify our guessing game so that we only allow the player a fixed number of guesses. Let's say 5.
Now open your guessing game program in Geany and modify it to look like this. Then save it as guess2.py.
Apart from the comments, the changes are that the while line is changed and we've added a print statement just below it. Be careful when you type it in and remember to indent the lines correctly.
If you run it you will get something like this
You see that it only allows you 5 attempts.
You will also see that it does not really do what we want it to when we get the answer right.
It doesn't stop when we get the right answer. But we will deal with that later.
The thing to concentrate on at the moment is the line
for i in [1,2,3,4,5]:
This is the line that controls the looping behavior. Unlike the while loop there is no boolean expression. Instead there is a variable, i, and a list [1,2,3,4,5].
A list an ordered sequence of values and is depicted in Python as a comma separated list enclosed in square brackets. The list we see above is a list of integers, but lists can be of any data type.
We can assign a list to a variable, by which I mean this
dayList = ["Mon","Tue","Wed","Thu","Fri","Sat","Sun"]
and we can access any individual member of the list like this
day = dayList
meaning that the variable day will get the value of the 5th item in the list dayList. So day will have the value "Sat". No, I haven't miscounted. Lists are numbered starting with 0 so dayList has the value "Mon" and dayList has the value "Sun".
The for loopSo the for loop looks like this
for i in [1,2,3,4,5]:
it's the word for, followed by a variable name, followed by the word in, followed by a list, followed by a colon. Below this are one or more lines of code that are indented - these are the lines that are repeated in the loop.
The length of the loop is the same as the length of the list, so, in the line above, the list is 5, so the number of repetitions will be 5, too. And for each repetition, the valiable (i, in this case) is given a value from the list. So in this case, in the first run through the loop i has the value 1, then 2, then 3, etc.
I've put the line print(i) as the first line in the loop just so you can see this working.
But how do we stop after we've guessed the right answer? Look at this.
You can see that I've remove the lines with the boolean variable guessed. We no longer need this. Instead, I've added the line break in the if statement that checks the answer. The break statement has the effect of unconditionally leaving the loop whether the conditions for leaving have been met or not.
In this case the loop is stopped when the correct answer is entered. (You can use this in a while loop, too.)
Tht's it for this time. I hope you enjoyed our little game.