A4 Worksheet - How to input numbers
A4 Worksheet - How to input numbers
This Python program is supposed to compute and display the age of the user, given their
year of birth.
1 print("Year of birth?")
2 birth_year = input()
3 age = 2020 - birth_year
4 print("You are", age, "years old")
If you run the program and type in your year of birth when you are prompted to,
you will be faced with an error message on line 3:
This is because input returns what the user has typed as a string, i.e. a piece of text. The
value of birth_year is a piece of text, so 2020 - birth_year cannot be evaluated.
Step 1
Modify line 2. This is how user input is converted to an integer value:
2 birth_year = int(input())
Step 2
Run your program.
If you were faced with an error message, these are some of the common errors that
may be responsible:
Your science teacher asks you to make a program that reads the user’s weight on Earth
and calculates how much the user will weigh on the moon.
You do some research and find out that gravity on the moon is a sixth (⅙) of what it is
on Earth.
Example
Note: Use these numbers to test that your program works correctly. In general, the result
displayed depends on user input, so it will not always be the same.
Step 1
Open this incomplete program (ncce.io/py-moon-20) in your development environment:
1 print("Weight on Earth?")
2 weight_earth = .
3 weight_moon = .
4 print("Weight on moon:", weight_moon)
Step 2
Complete line 2 so that the program receives input from the keyboard, after displaying
a prompt to the user. Make sure that the value assigned to the weight_earth variable
is an integer.
Step 3
Complete line 3 so that the program calculates the weight on the moon to be one sixth
(⅙) of the weight on Earth, i.e. one sixth of the value of the weight_earth variable.
Page 2
Task 2 . Your age in dog years
You are going to make a program that reads the user’s age and calculates how old the
user is in dog years. The common perception is that a human year is equal to 7 dog
years.
Example
Note: Use these numbers to test that your program works correctly. In general, the result
displayed depends on user input, so it will not always be the same.
The program displays the result. You are 35 years old in dog years
Step 1
Write your program, run it, and test it. Use the code from the worked example and the
previous task as a point of reference.
Tip
You will need to use:
* for multiplication
Step 1
Add these two lines of code to the beginning of the program:
Page 3
1 from time import localtime
2 year = localtime().tm_year
Explainer
Line 1 declares that the program will use a function called localtime, from the time
module. Modules are libraries of code that we can use in our programs.
Line 2 invokes localtime to retrieve the current year.
You can use localtime to obtain any part of the current date and time, including what
weekday it is (as an integer). Read the relevant documentation to find out more.
Step 2
Replace any occurence of 2020 in your program with a reference to the variable year.
Its value will always be the current year.
Resources are updated regularly — the latest version is available at: ncce.io/tcc.
This resource is licensed under the Open Government Licence, version 3. For more information on this
licence, see ncce.io/ogl.
Page 4