12: Iteration in Python

Debug it

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

Below is some code that has been deliberately broken so that you can fix it. 

It’s supposed to give the following program output for the Old McDonald Had a Farm nursery rhyme:

Challenges

  1. Change the mouse noise so that it eeks rather than moos

    Hint: The order of the values in the lists on lines 2 and 3 is important

  2. Add an empty line between each verse

    Hint: Indentation controls which lines are repeated.

  3. Change it so that it says the correct verse number rather than verse 1 each time

    Line 5 creates a variable called i which is used in the for loop to count from 0 to the length of animals (0, 1 and 2). You can use this variable to display the verse numbers you want (1, 2 and 3)

    Hint: remember to convert your integer to a string with str()

On the next page you’ll get some ideas for projects where you can use iteration to create your own projects

Leave a Reply

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