3 – In a while, crocodile


🎯 Learning Objectives

After this lesson you should be able to:

  • Use iteration (while statements) to control the flow of program execution
  • Perform operations on lists or individual items
  • Perform operations on strings or individual characters
💬 Key Vocabulary
  • Iteration
  • Boolean/logical expression (condition)
  • list operations (append, insert, pop, remove, index, count, reverse, sort, length)
  • list
  • list membership
  • index
  • list item

📖 Form a band

This program reads the name of an instrument and adds it to a list.

This action needs to be performed repeatedly, in order to add all the instruments required to form a band.

We could just copy and paste the same bit of code lots of times.

This approach won’t get us far.

How can we get our programs to perform actions repeatedly?

📝 Iteration recap

You need an iterative structure. (while) when your program needs to repeat actions, while a condition is satisfied.

Let’s modify this program together to use iteration (a while-loop), in order to repeat the same actions.

Follow along with your teacher to complete the code below.

📝 Activity 2 – City Hopping

Complete the tasks using the worksheet you can download below to create a random-trip planner in the Trinket window also below.

📖 Strings (are a lot like lists)

The built-in len function returns the length (number of items) of a list.

 Syntax:   len(list)

The len function returns the length (number of characters) of a string.

 Syntax:   len(string)

The in operator checks if a value is equal to any item in a list.

Expressions formed with in evaluate to either True or False.

 Syntax:   item in list

The in operator checks if a string is contained within another string.

Expressions formed with in evaluate to either True or False.

 Syntax:   string in string

An item in a list can be accessed through its index.

 Syntax:   list[index]

A character in a string can be accessed through its index.

 Syntax:   string[index]

📝 Activity 3 – City Guessing

  • Start with a program that uses the familiar list of European cities and complete the task on your worksheet to extend it into a city guessing game with hints.
  • As usual use the Trinket below to complete your code.

💬 Summary

In this lesson, you…
  • Used iteration (while statements) to control the flow of program execution
  • Practised using common operations on lists
  • Performed operations on strings
In the next lesson, you will…
  • Use iteration (for statements) to iterate over list items
  • Practise using common operations lists and strings

🏅 Badge it

🥈Silver Badge
  • Upload your completed Activity 2 – City Hopping worksheet to Bourne to Learn.
🥇Gold Badge
  • Upload your completed Activity 3 – City Guessing worksheet to Bourne to Learn.
🥉Platinum Badge
  • Upload your completed Activity 3 worksheet to Bourne to Learn as well as the Explorer task.