Skip to main content

3.15) Exercise 4 - Operations


Write a program where you assign variable 'a' a value of 5 and on the next line (one line only) perform the following operation:

  1. add 6 to a
  2. multiply the result by 2
  3. subtract 3
  4. subtract twice the original number a
  5. then halve the result
  6. and print the final result

What result do you get?
[tippy title="Solution"]
Answer: 4.5
Solution:

a=5
print float((a + 6)*2 - 3 - 2*a )/2.0

Explanation: When you halve the result, either multiply by 0.5 or divide by 2.0, if you divide by 2, the result will be the integer 4. The float() function I use in the solution is optional. Don’t forget to put brackets to prioritise operations. If you don’t get the correct result, try again.
[/tippy]