Try it on paper first. A hint, the key idea and the worked solution are here when you need them.
Here is a program.
Work out the right side of each line with the values at that moment. Then store the answer in the name on the left.
Have you tried it?
Work it out on paper first. It is fine to get stuck or make a mistake: that is how we learn. When you are ready, go through the solution one step at a time, and check each step against your own.
Plan. Do the job of the computer. Write down the value of each variable after each line.
| Line | a | b |
|---|---|---|
a = 2 | 2 | not made yet |
b = 3 | 2 | 3 |
a = a * b | 6 | 3 |
b = a + b | 6 | 9 |
a = b - a | 3 | 9 |
print(a, b) | shows 3 9 |
Check. In the third row, the right side a * b uses the old a, which is 2, so the new a is 6. In the fifth row, b - a is 9 - 6 = 3.
Answer: The program shows 3 9.
Common mistake. Using the new value of a variable in a line where the old one is still needed. Work out the right side first, with the values at that moment.
Log in to join the discussion.
Loading…