That Blue Square Thing

AQA Computer Science GCSE

24 March 2020: as the position regarding GCSE exams is now a bit clearer, I've decided to stop adding updates to the revision pages.

Programming Concepts - Print Statements

Print statements simply allow something to be displayed (printed) on the screen.

These are crucial to almost every program as they provide the main way to communicate information from the program to the user. This includes printing any instructions that the user needs to know about, telling them what data to enter and outputting the results of the program.

In general this is pretty straightforward stuff...

Simple Printing:

Pseudocode uses the command OUTPUT whereas Python uses the command print().

OUTPUT "Hello world"

Printing in Python needs to open and close the brackets correctly - otherwise you'll get a syntax error.

print("Goodbye cruel world")
print() # prints a blank line

Simple printing is really basic. Using blank lines to make output easier to read on the screen is a really good tip.

Printing variables is a little more complex, but works in pretty much the same way.

myString <- "Hello"
myInteger <- 73

OUTPUT myString
OUTPUT myInteger

The same sort of thing in Python:

myString = "Goodbye"
myInteger = 146

print(myString)
print(myInteger)

Really, the trick to to make sure brackets and quote marks are closed properly.

Concatenation:

Concatenation involves combining text and variables in the same print statement. It's an important way to make the output from your programs more readable.

So, instead of going:

OUTPUT "Your favourite country is"
OUTPUT faveCountry

You can go:

OUTPUT "Your favourite country is " + faveCountry

This prints the output over one line rather than two.

The biggest problem with concatenation is when you try and combine different types of data - in particular, trying to combine a string with a number. You can't do this immediately and have to do some conversion:

OUTPUT "Your test score was " + INT_TO_STRING(testScore)

This converts the integer value of testScore to a string so that it can be concatenated with the string "Your test score was ".

This is crucial to do and will cause a run time error in Python if you don't - which will crash the program.

The Python code here would be:

print("Your test score was " + int(testScore))

There is a lot more about concatenating properly on the Unit 3 String Handling page. This includes pseudo and Python code to handle concentating different data types.

Mutiple Line Printing

Sometimes you want to print more than one line of text. I often find this is the instructions at the beginning of a program or the options in a menu system.

There is a Python shortcut to reduce the number of instructions required in the program.

print("""Comedy Dog Top Trumps

Enter 1 to 4 to choose what you want to do:

1: Play a new game
2: Resume a saved game
3: View high scores
4: Exit the game"""
)

This will display all 8 lines of text in a single block and using a single print() command.

The three quote marks are what makes this possible. It's almost inevitable, by the way, that you will forget the closing bracket at the end of the code...