Monday 8 September 2014

Conversations with computers using python


I had the idea of making the standard 'Hello World' introduction to programming a new language a little bit more interesting for my Y7 class.

The idea of the computer conversations and the recent Turing test success or (near success) by Eugene gave me the idea of getting the students to make a (vastly simplified) version of Eugene using Python. The plan would be to teach them some basic Python concepts like displaying information and filling variables based on user input and then maybe progress onto selection.

This would initially start as a very simple program with them entering their name and then including the name in the response. The students could then work up some complexity from there using more questions. The next step would be to switch things round and have the computer answer user question based on a list of pre-programmed responses.

The Plan

Introduction

Show the class a video by way of introduction to the test. Something like - Jeremy Clarkson Explains the Turing test or The Turing test, as described by Expect Labs CEO, Timothy Tuttle.

Explain the plan to create a basic chat bot that can have a basic conversation with the user.

Task 1- Hello World

Students to open IDLE and the a new window. They then create a basic 'Hello World' program and save and run.

e.g. 

print ('hello world!') 

Students to experiment changing hello world for whatever greeting they choose. (Yes they will probably make it say rude words!)

Task 2 - Talk to me

Obviously this is a pretty one boring conversation so we need to add in the ability for the user to input information.

e.g.
myName == input('What is your name?')
print ('hello ' + myName)

Students to experiment with this and then try adding more questions.

e.g.
myName == input('What is your name?')
print ('hello ' + myName)
myColour == input('What is your favourite colour?')
print ('That is amazing ' + myName + ', ' + myColour + ' is my favourite colour too')

Task 3 - Selection

To try and make the computer's responses a little more realistic it would be good if the response wasn't the same what ever you type so we can add add selection to change the response based on what is input.

This can start with a simple if else:

e.g.
myColour == input('What is your favourite colour?')
if myColour == 'orange':
    print ('That is amazing ' + myName + ', ' + myColour + ' is my favourite colour too')
else:
    print ('It is nice that you like ' + myColour + myName + ' I prefer orange')

This can then be moved on to add more choice using else if (elif):

e.g.
myColour == input('What is your favourite colour?')
if myColour == 'orange':
    print ('That is amazing ' + myName + ', ' + myColour + ' is my favourite colour too')
elif myColour == 'black':
    print (myName + ' your are strange ' + myColour + ' is not even a real colour, how can it be your favourite?')
else:
    print ('It is nice that you like ' + myColour + myName + ' I prefer orange')

Task 4 - Ask me a question

This basic idea can then be used to switch things around and let the students ask questions. This will only handle a pre-programmed list of questions and answers but completes the very basic conversation idea.

This can be added to the first code or used to start a new program. if in the same program some of the old variables can be used to add more interest.

First the computer needs to prompt the user to ask a question:

myQuestion == input('Ask me a Question?')
if myQuestion == 'how old are you':
    print ('I am 12, how old are you ' + myName + ' ?')
elif myQuestion == 'What is your name?':
    print ('My name is Simon')
else:
    print ('Sorry i didn't understand that question')

Students experiment with their own versions.


This is a fairly simple program so it only has one question opportunity and only a couple of possible questions. If students still have time then they could be challenged to find a way to give more than one question opportunity or add further questions and answers. Another idea is for the students to program a combination of questions for the student and opportunities to answer questions like in a conversation, they could also look at getting the answers from a text file and possibly they could use the text file to allow the program to 'learn' by storing answers given by students to questions and then use those to respond when it is asked that question later!


Plenary

Show some examples of students programs to the class and use them to highlight the key parts of the program.


Further Resources

Since writing this I have found a short scheme of work based on the turning test on the Rapspberry Pi website http://www.raspberrypi.org/learning/turing-test-lessons/. This 3 lesson scheme explains the idea of the Turing test and uses a speech module to have the robot speak to you.