In an investigate task you take working code apart to see how each piece contributes to the result. Answer every question for yourself first. You can edit the code and run it to check. Only then open the answer.
Every answer below assumes you typed 7 when the program asked you for a
number. Run it with other numbers too, and watch which answers change and which
ones do not.
num1 = 25
num2 = 4
print("Enter a number:")
num3 = int(input())
total1 = num1 * num2
total2 = num3 + num2
total3 = total1 - total2
print(total1)
print(total2)
print(total3)
How many variables are used in the code?
Six: num1, num2, num3, total1, total2 and total3.
Each of them is a name on the left of an =, so each of them is a box with a
piece of data assigned to it. The three called total... are no different from
the three called num...; a variable that holds the result of a calculation is
still just a variable.
Identify the output statements in the code.
Four: print("Enter a number:"), print(total1), print(total2) and
print(total3). The first one prints the question the program asks; the other
three each output the contents of one variable.
What symbol is used for variable assignment?
A single =, with the variable on the left and the data on the right.
The data on the right can be a value you typed, such as 25, or something
Python has to work out first, such as num1 * num2 or int(input(...)). Either
way the right hand side is worked out first and the answer is then stored in the
variable on the left.
What value is stored in total1 at the end of the program?
100
total1 = num1 * num2 multiplies 25 by 4. Nothing later in the program assigns
anything to total1 again, so 100 is still what it holds when the program ends.
It is also the only one of the three totals that does not depend on what you
typed.
What value is stored in num3 at the end of the program?
7
That is the number you typed, as a whole number rather than as text, because
int() converted it on the way in. num3 is read on the total2 line but never
assigned to again, so reading it does not change it: whatever you type is still
in there at the end.
The line total1 = num1 * num2 is changed to total1 = num1 // num2. How
would it affect the program?
The output changes from:
Enter a number:
100
11
89
to:
Enter a number:
6
11
-5
25 // 4 is 6: floor division divides 25 by 4, which is 6.25, and then keeps
only the whole number part. So total1 becomes 6 instead of 100.
total2 is unaffected, because it does not use total1 at all: it is still
7 + 4, which is 11.
total3 is affected, because it does use total1: it becomes 6 - 11, which
is -5. Changing one variable changes every later calculation that reads it, and
leaves the rest alone.
Make sure that you check for the following things:
= for assignment.+ to add, - to subtract, * to
multiply, / to divide with a decimal result, and // to divide and keep
only the whole number part.input.