12: Iteration in Python

Extend it:

Make code repeat as many times as you want
Iteration in Python: Make code repeat as many times as you want

For this challenge you will need to use iteration to write a python program to display the following children’s song:

10 green bottles sitting on the wall
10 green bottles sitting on the wall
And if one green bottle should accidentally fall
There'll be 9 green bottles sitting on the wall

9 green bottles sitting on the wall
9 green bottles sitting on the wall
And if one green bottle should accidentally fall
There'll be 8 green bottles sitting on the wall

8 green bottles sitting on the wall
8 green bottles sitting on the wall
And if one green bottle should accidentally fall
There'll be 7 green bottles sitting on the wall

7 green bottles sitting on the wall
7 green bottles sitting on the wall
And if one green bottle should accidentally fall
There'll be 6 green bottles sitting on the wall

6 green bottles sitting on the wall
6 green bottles sitting on the wall
And if one green bottle should accidentally fall
There'll be 5 green bottles sitting on the wall

5 green bottles sitting on the wall
5 green bottles sitting on the wall
And if one green bottle should accidentally fall
There'll be 4 green bottles sitting on the wall

4 green bottles sitting on the wall
4 green bottles sitting on the wall
And if one green bottle should accidentally fall
There'll be 3 green bottles sitting on the wall

3 green bottles sitting on the wall
3 green bottles sitting on the wall
And if one green bottle should accidentally fall
There'll be 2 green bottles sitting on the wall

2 green bottles sitting on the wall
2 green bottles sitting on the wall
And if one green bottle should accidentally fall
There'll be 1 green bottles sitting on the wall

1 green bottles sitting on the wall
1 green bottles sitting on the wall
And if one green bottle should accidentally fall
There'll be 0 green bottles sitting on the wall

The starting point for your code just displays the first verse:

Challenges:

 
  1. Add the other 9 verses

    Whilst it’d be easy to just print out each line of the song, think about which bits repeat and what changes in each verse so that you can adapt it using for and while loops later

  2. Use a for loop

    The first line of each verse repeats twice. You could use a for loop to repeat it instead of printing the lyrics both times.

  3. Use a while loop

    You could make a variable called bottles and set it to 10. Your while loop could then keep decreasing bottles by 1 each time it repeats a verse until bottles gets to 0

Leave a Reply

Your email address will not be published. Required fields are marked *