Make It Interactive - Python Programming for Beginners (2015)

Python Programming for Beginners (2015)

Chapter Five: Make It Interactive

In this chapter, you will learn:

· How to receive and process input from users

· How to receive and process input from files

A computer program isn't much use if it just simply displays stuff you have wrote into the program. It is important to get input from the user of the program themselves in order to truly turn it into an actual computer program that people will use.

As we said earlier, Python is made to be simple and easy to use. With that in mind, Python chose a very good name for function which receives input from the user.

The Input Function

We're going to go all the way back to the information we put into the program in the second chapter, but this time we're going to ask the user for their name rather than making it up for them. In IDLE open a new file and input the following code:

#Ask the user for their name and age

user_name = input(“Can I take your name: ”)

user_age = input(“And your age: ”)

#Display the information on the screen

print(“Hello! My name is”, user_name, “and I am”, user_age, “years old.”)

Now save and run the program and you will notice that it asks you for your name and your age then displays the information to you.

The way the function works is by displaying the string you place in the brackets to the user then storing the information which the user inputs in a string.

Because the information is by default stored in a string, you will need to directly set the information to an integer or float if you wish it to be stored as such. You do that with the following code for an integer:

user_age = int(input(“And your age: ”)

And the following code as a float:

user_age = float(input(“And your age: ”)

Advanced Print

We know how to display text to our users by using the print function – and we know how to format the text. But there are a few extra advanced print functions you don't know about just yet!

The first advanced print function is to display the message over more than one line using the “triple-quote” symbol, which is simply “” or “””. An example of how to use this:

print (“”Welcome to this program.

I will need to know your name

on the next page.””)

This will print the message over the three lines, which makes it easier to read.

The second advanced print function is to use “escape characters”. Escape characters allow you to print characters which are “difficult to type” like tab, new line or a quotation mark. You are able to print these characters by using a \ (backslash) character.

To type a tab you use the code: \t.

For example, print (“Look at this\tTAB!) will display:

look at this tab!

To type a new line you use the code: \n

For example, print (“Look at this\nNew Line!”) will display:

Look at this

New Line!

To type a backslash you use the code: \\

For example, print (“Look at this \\ backslash.”) will display:

Look at this \ backslash.

To use a double quote or a single quote you use the code: \” or \'

Which one you need to use an escape character for will depend on whether you use single quotes of double quotes in your text. If you use single quotes then you can display a double quote with no issue, but a single quote will end the string. Vice-versa if you use a single quote.

As an example, print (“Hey, I can display a \” today.”) will display

Hey, I can display a ” today.

If you want to use the letter after the \ to be counted as a special character you need to add an r before the first quotation mark. For example, print (r“Look at\this.”) will display:

Look at\this.

While, print (“Look at\this.”) will display:

Look at his.

Receive Input From Files

Receiving input from text files is very easy. First, create an example text file, type out 5 lines of text and save it as exampletext.txt to the same folder you are going to save the program to. To open the text file you simply use the following test:

#Opens a file

text_file = open ('exampletext.txt', 'r')

This line will open the file named in the brackets and set it to a read-only file with the 'r'. You can open the file for reading ('r), writing ('w'), reading and writing ('r+') or appending ('a'). If you are writing or appending (adding data to the end) to a file which does not exist, your program will create the file.

Once you have opened the file you need to read from the file with the following code:

#reads lines from file and displays them on the screen

firstline = text_file.readline()

secondline = text_file.readline()

print (firstline)

print (secondline)

When you use the readline() function it will read the next line from your file. Which means that this code will display the first two lines of your file.

Once you have finished reading from the file you want to close the file to save up space in your program. Which you do with this code:

text_file.close()

If you choose to write to a file, you open and close the files in the same way, but what you do in between is different. If you want to overwrite everything currently contained in a file then you need to use 'w' when you open a file, if you just want to add to the file then you need to use 'a'. You write to the file using the same code whether you are writing or appending a file. Here is the code to write to a file. I have chosen to but you can choose to write if you wish:

text_file = open ('exampletext.txt', 'a')

#writes text to the file

text_file.write('\nThis will be added to the end of my file.')

text_file.write('\nAs will this.')

text_file.close

You will have noticed the new line escape character \n in the text above, if you fail to add that escape character then the text will run on in the same line. As text is read line by line in Python you want to ensure that you write line by line when you need to.

In the next section we are going to start controlling the flow of your program. Which is when things get very interesting!