Not returning the answer expected, says something about exit, but no answer

maths_operation = 1 + 3 * 4 / 2 - 2
print(maths_operation)

Not returning the answer expected, says something about exit, but no answer

Returns answer = 5.0

Repl link:

:wave: Hey @jlp203, welcome to the forum!

Can you please provide a link to the Repl? This way it is easier for staff and members of the community to help you!

Also see this guide on how to share your code:

2 Likes

I believe this is right…
First you evaluate the multiplication and division in left - right order which gives you
maths_operation = 1 + 6 - 2
And then addition and subtraction left-right:
1 + 6 = 7
7 - 2 = 5

Just remember that sometimes computers don’t follow order of operations, which may give you different results.

It should be right. I copied it exactly but it would not execute correctly. Kept returning an error.

JLPARTIDA

Hmm…

  • Whats the error?
  • Maybe try print(str(maths_operation))?

Is it not supposed to print 5.0? or is that expected result but it’s not being given.

In python, operations are done as this:
brackets (inside brackets → outside brackets)
multiplication/division(order depends of which one is more front)
Addition/subtraction (also depends of order)

In your example, it contains no brackets, so it do multiplication&divisions first
First encounters 3*4, =12
Then encounter /2, 12/2=6
And then addition&subtractions 1+6-2, =5

Because you used division with decimal points (/), it just make the result of the division into float (must have decimal point), therefore it is 6.0 instead of 6
And then the result will be 5.0 (in float)

If you don’t want the decimal points, use round division (or whatever that’s called) (//), it force the result to be an integer (without decimal points), which if the result is float it just rounds the result.

Also, if you want to prioritise some operations (eg. (3*4)/(2-1), which is equals to 12, no brackets results into 5 (changed the last 2 into 1 so there is no ZeroDivisionError)), use brackets

2 Likes