Although this code still works, in Python 3.6 this has been replaced by f strings.
For iGCSE we now recommend using the rounding function.
For iGCSE we now recommend using the rounding function.
code: TU03 Points: 10
How to print mixed strings and numbers
There are many ways to print strings and numbers in Python. This method enables you to mix and match strings and numbers as you please.
MyAge = int(input('What is your age?'))
print('I am younger than %d' % MyAge)
Notice how we put in the placeholder %d in the text and then after the speech marks we add another % and then the variable MyAge
MyAge = int(input('What is your age?'))
print('I am younger than %d' % MyAge)
Notice how we put in the placeholder %d in the text and then after the speech marks we add another % and then the variable MyAge
Exercise 1: Tell the person that MyAge "Is a very nice age to be"
Multiple Variables
The real benefits of this method come into play when you use more than one variable.
Notice how we can include multiple variables. There are some very useful things to note:
%d prints any whole number.
%s prints a string
%.2f prints a number to two decimal places. (You can choose how many decimal places to display to.)
Have a look at the code below:
%d prints any whole number.
%s prints a string
%.2f prints a number to two decimal places. (You can choose how many decimal places to display to.)
Have a look at the code below:
Exercise 2: Change the code so that the computer has an age to 3 decimal places and is older than you.