Lesson 2 – Introduction to Programming and Building a Chatbot

Learning Outcomes

  • Create a “Hello World” program in Python
  • Identify and correct common errors in Python programs
  • Create, save and test a ‘Hello World’ program
  • Build the first part of an artificial intelligence program using Python
  • Use the Python Interpreter as a calculator and explain what an integer is

Keywords

Words to learn: programming, instructions, syntax, execute

Main 1 – “Hello World”

Over the next few lessons you are going to create an artificially intelligent chatbot like Siri using the Python programming language.  You have already used a programming language – Scratch.  Python is also a programming language but it uses text instead of blocks to construct a program.

To begin we need to load up the Python development environment – it’s called IDLE.  To load it go to the Start menu -> Python 3.4 -> IDLE (Python GUI)

When IDLE loads up it starts up in the Python “shell” which means anything you type it will do straight away.  Type the following in where there are three chevrons >>>

print("Hello World")

The phrase Hello World will appear in blue just under where you have typed that instruction:

yr7_python_lesson1_img1

Well done, you’ve just done your first bit of coding in Python!

Main 2 – Syntax Errors

Let’s try something a bit different – try typing in the following where there are three chevrons >>>

print(Hello World)

You will get something a bit different:

yr7_python_lesson1_img2

Computers are machines that are very literal when it comes to following instructions; humans are rarely so literal. Computers are not as good dealing with nearly, almost, and not quite in the way that we humans are. Syntax is used to describe the rules that determine the way that instructions and commands must be written. Python is reputed to be more forgiving of syntax errors than many other programming languages, which can make it easier to learn, meaning more time can be spent creating than debugging.

IDLE, the Python IDE has syntax highlighting which automatically assigns colours to different elements, e.g. the “Hello World” phrase should be green. This should help spot some simple errors when typing in commands.  In the example above, when you typed the first piece of code, because you typed it correctly it turned green.  In the example you just did the word World was highlighted and you got a red error message.

To see what does work and what doesn’t, try the instructions below:

print"Hello World"
print("Hello World");
Print("Hello World")
print("Hel World")
prin(Hello World)

What happens – which ones work and which ones don’t?  For those that don’t work why do you think that is?  How does IDLE help you?

As you can see, programming languages (just like real languages) have rules – unlike human languages where we can apply reasoning and experience to work out what someone means, a computer will simply reject invalid syntax and give you an error.

Main 3 – “Hello World” revisited

So far Python has carried out instructions straight away in the “shell”.  What we want to do now is to create a set of instructions – so we need to use the code writing section of IDLE.  To do this go to the File menu and choose New Window.  A new window will open with no text in it.  Let’s sort that!  Type the following:

print("Please type your name in")
my_name = input ()
print("Nice to meet you " + my_name)

You now need to save the file before we tell Python to run these instructions.  Go to the File menu and choose Save As – click on Libraries and then Documents.  Give your program the name of nameasker.py then click Save.  To run your program go to the Run menu and choose Run Module.  Your computer will switch back to the Python shell and execute your instructions.

What does the program do?  Give it a go!

Main 4 – Building an artificial intelligence program in Python

We are now going to adapt your code into a chatbot program that appears to be intelligent.  To do this we need to consider what people will ask.

In a pair – If you meet someone for the first time discuss what questions you would ask?

If we can get our chatbot to lead the conversation, then we can give the impression of intelligence.  For example, let’s try and get the computer to do the following conversation with a human:

Computer: Please type your name in
Human: Bob
Computer: Nice to meet you Bob
Computer: So, Bob, what is your favourite food?
Human: Fish and chips
Computer: Ah, your favourite food is Fish and chips - mine too!

So the Python code would look like this:

print("Please type your name in")
my_name = input ()
print("Nice to meet you " + my_name)
print("So, " + my_name + " what is your favourite food?")
favourite_food = input ()
print("Ah, your favourite food is " + favourite_food + " - mine too!")

Try getting your chatbot to ask a few more questions from the list you came up with earlier – make sure you test it each time to make sure it works.  If you get a syntax error, try and work out what has gone wrong and fix it.

Level 6 challenge: If you’ve found that easy, try and get your chat bot to summarise all the previous replies at the end of the conversation.  For example at the end it could say “So Bob, it was lovely to meet you. I now know that you live in Belper and that your favourite food is Pizza”.

Once you have completed this task, save your Python program as chatbot.py and print out the code to put into your folder.

Quick Assessment Levels

Level 4 – your code asks the user their name and favourite food and possibly one or two other questions
Level 5 – your code asks the user their name, favourite food and a few more questions
Level 6 – your code asks the user their name, favourite food, a few more questions and then summarises the replies at the end of the conversation

Plenary – Using Python as a calculator

Go to the Python Shell window.  We can get Python to carry out mathematical instructions.  See if you can get the Python Shell to work out the following:

156 add 567
132 subtract 46
256 divided by 8
389 multiplied by 13

Top tip: you have used mathematical symbols before in Microsoft Excel…

We can also use variables to store numbers.  Try typing the example below into the Python Shell to see what happens if we use variables to store numbers:

pizza = 250
coke = 100
chips = 150
pizza + chips
2 * pizza

What happens?  Can you explain the answers the computer gives?  What does it remind you of in maths?