That Blue Square Thing

AQA Computer Science GCSE

Programming Concepts - Input Statements

Input statements allow users to enter data to be used in a program.

This is a really fundamental area of coding. Without the ability to enter values programs aren't very flexible. It's crucial that you understand input statements and how they work.

One key idea is that you want to "capture" the values being entered. This means that you need to assign them to a variable.

The pseudocode to allow user input is straightforward.

catName <- USERINPUT

If you need to output a message with the input line you need to do that using an OUTPUT statement on a separate line:

OUTPUT "Enter the cat's colour:"
catColour <- USERINPUT

In Python the message and input can be combined on the same line.

yourName = input("Please enter your name: ")
yourAge = input("Please enter your age: ")

print() # print a blank line
print("Name: " + yourName) # one way of printing on one line
print("Age", yourAge) # another way of printing on one line

The key thing to remember is to always assign an input value to a variable.

Inputting numbers

Inputting numbers is slightly more complex.

Python always assumes that anything inputted using an input statement will be a string - i.e. a word (see more about data types). This includes what you might think of as a number. Python sees "42" and things it's a word made up of the character '4' and the character '2'. It absolutely doesn't think of it as a number.

This means you can't do things like this:

# this code won't work!
yourAge = input("Please enter your age: ")

yourAge = yourAge + 10 # add 10 years to yourAge
print(yourAge)

To solve this problem we have to tell Python that we want to consider the input as a number. We can do this by converting the input to a number - often to an integer (a whole number).

Here's how to do that:

# this code will work!
yourAge = input("Please enter your age: ")
yourAge = int(yourAge) # convert to an integer

yourAge = yourAge + 10 # add 10 years to yourAge
print(yourAge)

Sometimes you want to use a decimal number rather than an integer. Now you have to convert to a floating point number (a float data type).

yourShoeSize = float(input("Please enter your shoe size: "))

print(yourShoeSize)

Note that this time I converted at the same time as the input happened. This is fine to do with integers as well - but take care to include the right number of closing brackets (I reliably miss one off and end up with a syntax error on the next line down).

Printing Numbers

It's all very well converting inputs to numbers, but you need to be a little careful when you print them out again.

Python can cope with a simple print statement with a number in it, like this:

print(yourAge)

But if you want to print the number out on the same line as words you need to be careful how you do it. Take a look at the code below to see.

yourAge = int(input("Please enter your age: "))

yourAge = yourAge + 10 # add 10 years to yourAge

print(yourAge) # works
print("Age:", yourAge) # works
print("Age: " + yourAge) # does not work
print("Age: " + str(yourAge))# works

The final line of code works because I've used str() to convert the number back to a string. Whenever you use the + in a print statement you need to use str() if there are numbers involved.

So why use + in a print statement? It doesn't leave a gap after the words, unlike a comma. So, if you want to print $5 without a gap you need to use + and not a comma.