< Prev  |  Home  |  Next >

Python File Handling

In this section, we’re going to improve our BOB chat bot. In the last version we made (BOB V3) we used a load of ‘elif’ statements, to respond to whatever the user types in. Go back and look at your solution to BOB V3 if you’ve forgotten!

For the new BOB V4, we’re going to create a text file called “data.txt” and add some possible questions that BOB might be asked, along with his(?) response. Open Notepad (if you’re using windows) and type in the following:

Hello: Are you OK?
Yes: Glad to hear it.
No: Oh that's a shame

Save the file in the same place that you save your Python programs, and call it “data.txt”.

Firstly, let’s write a program to check that we can read in the data:

You should see the file printed just like it appears in your text file. I’ll just explain a couple of things before we move on to make the program more interesting. Firstly, in the line:

The “r” just means that we can read the file. You’d use “w” if you want to write to the file, which we’ll do later. Also, the variable “fi” in the program isn’t a string, it’s a file, which we can open, close and read from.

When we printed each line, we stopped the print function from printing another new line after the one already in the file:

You can end the print function with anything you like. The default (standard) option is a new line, but in this case we’ve ended each line with nothing.

Let’s use what we’ve learnt so far. We can say something to BOB, and BOB can reply with an answer. For us to do this, I’ll just explain the data in the text file. In, for example, the line:

Hello: Are you OK?

The bit of the string before the colon (:), in this case “Hello”, is the question that BOB might be asked, and the bit after the colon is his reply (” Are you ok?”). So if we type “Hello” to BOB, he should reply with ” Are you OK?”. To do this, we need to split each line of the text file one at a time, by using something like:

This splits up the string into two parts, the question and the answer.

Here’s the program:

Watch what happens when we type something that is in the file (“Hello”) and something that isn’t (“Do you like me?”):

>Hello
Are you OK?

>Do you like me?
>

If the user enters something that BOB doesn’t understand, why don’t we try and make BOB learn the correct response? To do this, we need a variable called “found”, which is initially set to False. The program should check all of the lines in the text file, and if it finds the question, the variable should be set to True.

This means that after it has scanned all of the lines of the text file, either it has found a response (found == True) or it hasn’t (found == False). If it hasn’t found a response, BOB should get one from the user, and append the answer to the text file (append means add it to the end):

So now, BOB actually learns to talk to its user! In the evidence below, watch how BOB has no idea how to answer the user saying “haha” the first time, but then learns it for the second time:


>hello
Are you OK?

>yes
Glad to hear it.

>haha
how would YOU answer that? : That's not funny.
>hello
Are you OK?

>yes
Glad to hear it.

>haha
That's not funny.

If you talk to BOB for long enough, I’m sure you could have a proper, meaningful conversation! In the program above, we used:

To append the new sentence to the end of the text file. Have a look if you like; you’ll see the text file is unchanged apart from the new question/answer that has been added at the end.

If you wanted to just overwrite the old file with new stuff, you would use “w” for write instead of “a”.

Challenge

Remember the “double or quit” program we created earlier? See if you can add to the program, to save the highest score to a text file, along with the name of the person who achieved the score.

As you’re only saving one score/name, you can use “w” to write over the file if a new high score is achieved. Before you start developing your program, create a file called “score.txt” with the following line in it:

0:No Name

Also, as you only need to read one line from the file, you can use “readline” instead of “readlines“)

< Prev  |  Home  |  Next >