9_1_3

WHAT: Determine the purpose of algorithms

SMART Start

What would this python code display when run?

Extra Mile

What would happen we changed the name of the variable to something different? Why?

HOW:

Activity 1 - READ

An essential part of computational thinking is being able look at algorithms and figure out how they work and what they do. This skill is helpful when you are working on a programming team or using old pieces of code to form a new solution.

Here is some code from a simple application.

import time 
import random 
print ("Welcome to my fortune teller") 
question = input("Would you like me to tell you your fortune? ")
colour = input ("Think of a colour ")
print ("I will now select a fortune based on your selection of ",colour)
time.sleep(2)
fortunes = ["You will win some glasses", "You will meet a special someone", "You will have an embarrassing incident"]
print (random.choice(fortunes))

Without any further guidance, you should be able to look at this algorithm and determine it's purpose. A good place to start would be to identify the inputs, processes and outputs.

Here is a sample purpose statement for the above algorithm:

Time & Random have been imported and are used later in the algorithm. The first output is a title saying "Welcome to my fortune teller". This makes me think that the application is most likely a fortune teller. There is then some input asking if the user wants to have their fortune told. They could type anything and it would go to the next instruction which is to type a colour. Again, they can type anything and it will still work. There is a pause of 2 seconds and a fortune is randomly selected from the fortunes array.

Therefore...The purpose of this algorithm is to predict the user's fortune.

Activity 2

Look at the code below:

import random
testing=False
name=input("What is your name")
print("Hello " + name)
myWords=["tree","bark","apple"]
word=random.choice(myWords)
print ("I spy with my little eye something beginning with...")
print (word[0])
print ("The word has "+str(len(word)) +" letters")
wordLength=(len(word))
line=""
for x in range(0,wordLength):
    line+=" _ "
print (line)
if testing:
    print (word)
attempts=2
guess=""
play=attempts>0 and guess!=word
if testing:
    print (play)
while play:
    guess=input("Make a guess")
    if guess!=word:
        print ("Wrong")
        attempts-=1
        play=attempts>0 and guess!=word
    if guess==word:
        print ("Correct")
        play=attempts>0 and guess!=word

You might want to copy and paste this code into Python to help you determine it's purpose.

Look at each part of the code and try to figure out how it works and what it is doing. Write your purpose down. The example in Activity 1 might help you with your explanation.


CHECK:

EMBED:

Try to determine the purpose of this algorithm:

import random
testing=False
name=input("What is your name")
print("Hello " + name)
myWords=["tree","bark","apple"]
word=random.choice(myWords)
print ("I spy with my little eye something beginning with...")
print (word[0])
print ("The word has "+str(len(word)) +" letters")
wordLength=(len(word))
line=""
for x in range(0,wordLength):
    line+=" _ "
print (line)
if testing:
    print (word)
attempts=2
guess=""
play=attempts>0 and guess!=word
if testing:
    print (play)
while play:
    guess=input("Make a guess")
    if guess!=word:
        print ("Wrong")
        attempts-=1
        play=attempts>0 and guess!=word
    if guess==word:
        print ("Correct")
        play=attempts>0 and guess!=word

Copy and paste this code into Python to help you determine it's purpose.

Extension Activity

Improve the application in the example above.

CLASSROOM IDEAS:

This outcome may work best as a paired activity to help students discuss the code together.