If you've followed on from Chapter 1, you might recall I asked you to "Write a Python script that prints the days of the week".
Did you do it?
You should have ended up with something that (roughly) looks like this:
Don't worry if yours isn't the same. There's various ways you could have completed the task. For example, you might have just dumped all the print statements in main.
The main point is, did it work?
You should have ran this at the cmd line, and got output similar to the following:
Smashing, lets move on.
Nobody likes repetitive tasks. So copying and pasting (or typing in) all of those print statements and changing the day was a bit naff.
Lets see a different way of doing it.
The above script does exactly the same thing as the one at the begginning of this chapter. It does however use 2 components you haven't seen before.
A list is just a collection of items.
In this script our list is a collection of the days_of_week .
A loop just repeats itself until its told to stop.
In this script we just repeat ourselves until we've seen every day in the day_of_week list.
Each time the loop repeats day is given a different value.
So the first time it has the value 'Monday', the second time 'Tuesday' and so on..
You won't learn Python, or programming in general, by just reading tutorials. You actually need to get your hands dirty.
So...the task for this chapter is to update your existing program to look like the one above.
Have a coffee first though. What's the rush?
If you found this useful, please share with friends.comments powered by Disqus