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:
- add 6 to a
- multiply the result by 2
- subtract 3
- subtract twice the original number a
- then halve the result
- 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]