# Iteration Exercises

On this page you will find a range of additional exercises that will help you practice iteration. Some of these exercises will be quite challenging if you are new to programming.

## Formatting Strings

Previously, you were introduced to the .format() function which allows you to place variables into your string in a very beautiful way.

For example, if we had the following variables:

• name = "Beth"
• score = 10

We might want to present the output in the following way:

• "Beth scored 10 points."

Using the .format() function the code would look like this:

• print("{0} scored {1} points.".format(name,score))

This is great, but imagine we had a list of people and scores to display. In the real world we might use a table to do this:

Name Score
Beth 10
Matthew 8
Fraser 7

In Python this might look like the following:

 1 2 3 4 print("name score") print("{0} {1}".format(name1,score1)) print("{0} {1}".format(name2,score2)) print("{0} {1}".format(name3,score3)) 

Unfortunately, this is not very tidy as the result would be:

It would be better if it looked like this:

You could spend ages messing around adding spaces into your print statements or you could do this:

 1 2 3 4 print("{0:<10} {1:<5}".format("Name","Score")) print("{0:<10} {1:<5}".format(name1,score1)) print("{0:<10} {1:<5}".format(name2,score2)) print("{0:<10} {1:<5}".format(name3,score3)) 

Notice, that each {} placeholder now has additional control characters. These specify the width of space that is given to each placeholder and the alignment of the characters within that space.

The < character represents left alignment and the number 10 the amount of spaces that are provided for the placeholder to fill. The following alignments are available:

Character Alignment
< left alignment
^ centre alignment
> right alignment

You can find out more about string formatting and the formatting mini-language in the Python Documentation.

## Basic Exercises

1. Create a program that will ask the user for a number and then print out a list of number from 1 to the number entered and the square of the number. For example, if the user entered '3' then the program would output:

• 1 squared is 1.
• 2 squared is 4.
• 3 squared is 9.
2. Create a program which will produce the times table for an number entered by the user.

3. Write a program that will add together a series of numbers until the user enters a rogue value of 0. The program will then display the total.
4. Create a program which will count down from 10 to 0, indicating how long there is to go before time runs outs. When time runs out it should display a suitable message.

## Linear Search

Linear Search is a simple algorithm that you can use to search for an item in a string or list. It can be expressed in psuedo-code as:

  1 2 3 4 5 6 7 8 9 10 11 element ← 0 found ← False lengthOfList ← len(listOfItems) WHILE (found = False) and (element < max) DO IF searchItem = listOfItems[element] THEN found ← True ...other statements... ELSE element ← element + 1 END IF END WHILE 

This linear search algorithm is based on one provided for the A-Level Computing specification. It is possible to modify this algorithm to find more than a single occurrence of an item in a list...There are also far more efficient algorithms for this purpose but knowledge of linear search is a requirement at A-Level.

## String Slicing

Sometimes it is necessary to return part of a string - we can use string slicing for this purpose. Some examples are shown below:

  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 phrase = "Apple Sauce" print(phrase[0]) #prints the first character of the phrase print(phrase[-1]) #prints the last character of the phrase print(phrase[1:4]) #prints the characters 'ppl' #(from index 1 up to but not including index 4) print(phrase[2:]) #prints the characters 'ple' (from index 2 to end) print(phrase[:4]) #prints the characters 'Appl' #(everything up to but not including index 4) print(phrase[1:8:2]) #prints the characters 'pl a' #(every 2nd character from index 1 up to but #not including index 8) 

## (Hopefully) Challenging Exercises

1. Create a program that uses linear search to check that a given character is in a given string.
2. Extend the previous exercise so that it will display the string showing instances of the provided character and blank characters for the remaining letters. For example, if the string was "hello" and the user entered "l" the program would display " _ l l ".

## (Hopefully) Very Challenging Exercises

1. Create a program which will convert a given decimal up to 255 into its 8-bit binary equivalent.
2. Extend the previous exercise to convert the binary number to hexadecimal.
3. Create a program to convert from hexadecimal to decimal.

## Difficult Exercises

1. Extend (Hopefully) Challenging Exercise 2 so that it will allow you to check for another character (until a rogue value is entered) and displays an appropriate message based on the result. For instance, if you entered 'e' you would see " e l l _ " and if you entered 'm' you would see "m is not in " _ l l _".
2. Improve the previous exercise so that it is a complete game of hangman for two players which gives a set number of guesses to the user and displays an appropriate message for the winner.