Python
Programming
Lesson 3 - Variables
Task 1 - Calculator
Create a simple program that allows you to enter two whole numbers, it will then multiply them together for you, follow these steps to do it. You should code your program in the trinket below.
1. Use the input command get to the program to ask for two numbers from the user. It should store the first number in a variable called num1 and the second number in a variable called num2. It should look like the example to the right when run.
2. Add the following code to your program:
total = int(num1) * int(num2)
3. Now print the result of the calculation so it looks like this when run:
What this does
A new variable called total is created that multiplies num1 by num2.
As the variables are whole numbers they must be converted to an integer before carrying out the calculation, therefore each variable is inside the int() function.
NOTE: the answer should change depending on what two numbers are entered.
Extension
Add three more calculations and three more print messages so it tells you:
What the two numbers added together are
What number 2 subtracted from number 1 is
What number 1 divided by number 2 is
An example of what it could look like is shown to the right.
Task 2 - Dog Years
You need to create a program that works out a human age in dog years, or a dogs age in human years. An example of what the program should look like when run is shown to the right.
Use the success criteria below to help you:
Use input to ask the user to “Enter the age in years” and store it in a variable called age
Calculate the age in human years by creating a variable called humanyears and divide the age by 7
Calculate the age in dog years by creating a variable called dogyears and multiply the age by 7
Use print to create an output that looks something like the example to the right.
REMEMBER: you will need to convert the variables from string/integer/float using:
str(variableName)
int(variableName)
float(variableName)
Extension: Rounding a variable
Make sure that the humanyears variable is rounded to 2 decimal places inside the print command. To round a variable to 2 decimal places, you must use the round function:
round(VariableName, 2)
How it works:
round(VariableName, 2) - The name of the decimal variable you want to round.
round(VariableName, 2) - The number of decimal places it will round to.
Below is an example when the variable is/isn’t rounded to 2 decimal places.
Not Rounded
Rounded
Evidence point
Open the Assignment in Google Classroom called Student Evidence - L3 - Variables.
Take a screenshot of your code for:
Task 1 - Calculator
Task 2 - Dog Years
For each program click on Share and choose Link. Copy the link and paste it into the document, this means you can come back to it in future lessons.