9_3_2

WHAT: Apply nested selection and nested iteration to a given problem

HOW:

Activity 1

Selection is making a decision based on whether a condition is true or false.

Iteration is looping or repeating.

Nested one inside another.

Example nested if statement:

if 1>0:
    if 10<20:
        print ("Hello, world!")

Essentially here we are saying... IF 1 is more than 0 AND 10 is less than 20 THEN print "Hello, world!").

Example selection in a loop:

for x in range(0,10):
    print (x)
    if x == 2:
        print ("Found the 2")

This piece of code loops through the range and prints the value of x. IF it reaches the number 2 it says "Found the 2"

Example nested iteration (loop within a loop):

myArray = ("A","B","C")
for x in range(1,11):
    for item in myArray:
        print (str(x)+item)

This pieces of code creates an array with three items inside. It then runs a loop 10 times. Within each iteration of the loop, an item from the list is added to the end of the number.

play = True
while play:
    for x in range(0,10):
        print (x)
        if x == 9:
            play = False

This code is pretty inefficient but it shows you selection within a for loop within a while loop which is pretty cool!

Run these four code snippets to see them in action!

Activity 2

If you want to see this in a game then we can look at the usual, simple example of a guess the number game:

import random # allows you to randomise number selection
number = random.randint(1,100) #selects a random number between 1 and 100
guess = ()
while number!=guess:
    guess = int(input("Guess :"))
    if guess > number:
        print ("Lower")
    elif guess < number:
        print ("Higher")
    elif guess == number:
        print ("Correct!")

Try the code yourself!

Activity 3

Have you ever seen a chatterbox?

If you haven't then take a look at this video to see how they work.

Make your own chatterbox to help you understand how they work.

Activity 4

Download a computational thinking problem solving sheet.

Try to find a computational solution for your chatterbox using the sheet that you have just downloaded. If you don't know how to use the sheet then you can view the Rock, Paper, Scissors demonstration from 7_3.

Need some help??

Possible code snippets:

  • Loads of nested if's - eek!
choice=input("Choose a number")
if choice=="1":
    choice=input("Choose a colour")
    if choice == "Red":
        choice=input("Choose a letter")
        if choice == "A":
            print ("You have a secret admirer")
  • Simple but not very random or authentic!
options = ["Number","Colour","Letter"]
for item in options:
    print ("Pick a "+(item))
    choice=input()
print ("You will get married on a Beach")

CHECK:

EMBED:

Test and Evaluate the algorithm that you have created for your chatterbox.

  • Does it still work if there is an input error?
  • Could it give more feedback to the user to make it more realistic?
  • Are there enough fortunes available to the user?
  • What does a test user think of your chatterbox? Do they have some advice for you?

After testing and evaluating, improve your chatterbox by creating a version 2 of the code.

CLASSROOM IDEAS:

The chatterboxes could be made as a homework task if you want to save time in the lesson.