A-level Computing/AQA/Paper 1/Fundamentals of programming/Input and output





An important part of computer code is allowing your user to input data into the program, things such as text, key presses or even a data feed from a motion sensor games controller.

Once the data is in the program you want the system to output responses, these may take the form of output to a screen: graphics, text, output to a sound device, printed documents or data output to another program.

For this unit you need to be familiar with a few input and output commands. The little black box that appears on your screen is called the console. In VB.NET all the commands to read and write from this are accessed from the  command, let's take a look at some:

Outputs
For the AS course it is unlikely you will need anything beyond the print to screen command and the write to file command (see later). In VB.NET there are two write to screen commands that you should be familiar with: Let's see an example of this in action: This would output the following:

Hello how are you?

I'm fine thank

you.

Notice the difference between a  and a   command.

Python does not have two separate commands in the way VB.NET does. The  command will by default add a new line on to the end of each string. Here's an example:

will produce the output:

This is the first line. This is the second half of the first line.

This is the second line.

If you want to output several things all on the same line you have to add  to the print command. This tells Python that instead of ending with a new line you instead want to end with nothing. So the following example outputs all on the same line:

This output will all appear on the same line.

We can do a lot of things with the  command. If you are using Visual Studio then type the following: Up should pop some suggestions. Play around with  and

There is also a command to output a small and annoying sound, it's unlikely you'll need to know this for the exam, and your teachers will most likely hate it.

Find out more about the  command on the MSDN website

Inputs
To make your program interactive you'll need your user to input commands, for the exam these will most likely be text instructions or reading from a file (covered later). You might look at buttons and games controllers next year. In VB.NET there are two types of input command that you need to know: Let's take a look at a quick example of where this might be used:

Please insert you name: Ali Hello Ali

There is also the command that reads in a single character. Take a look at this example:

Please select from options 1,2,3 or 4:

4

You chose : 4

In Python there is just the one command for reading user input from the keyboard which is. Here's an example:

which if you type in your name as Alice will give the following:

Please enter your name:

Alice

Hello Alice

What does the following code output:

The

Catsat on

the mat

The average age

of a combat soldier in Vietnam was 19

Write code to display the following to the screen:

My favourite colours:

Red

Blue

What would the following look like: for the input:

John

16

Enter your name: John

Enter your age: 16

Hello 16. You are John years old

Whoops! Fix this: