# Try This In Area Calculator, Don't Know Why The Results Are Difference?

#1

When I typed the code as area = (1/2)baseheight, why did the calculate area = 0.0000 and the program did not say anything about what I did wrong?

With a little bit of typo change from the above into area = 0.5 baseheight, why this one returned the correct area value and not the other?

I did try both way, just want to know what’s the difference because in normal math, 1/2 == 0.5 and people can use them to replace one another just fine, but in Python is difference. I also include to 1/2 and make it look like this (1/2) to tell the program that it has to calculate 1/2 as one set before it moves on to multiply (1/2) with base and height. If you confused what I am trying to say is I include () around 1/2 to tell the program so it know which order of operation come first.

#2

freeform projects do not have any validation.

``````print 1 / 2
``````

you will see that the result is zero. In python2, a division that involves two integers (non-decimal numbers) will give an integer as result, if needs be, python will round down to the nearest integer

this has one advantage which i can think of from the top of my head:

``````the_list = ['a', 'b', 'c']
# print middle element from the list
print the_list[len(the_list)/2]
``````

given the length (3) divided by 2 gives 1 (rounded down), this is possible. Would this give 1.5, the above code would give an error

but its not normal division behavior, so thankfully they fixed it in python3

#3

In Python 2 we need one or both operands to be a float.

``````1. / 2       => 0.5

float(1) / 2 => 0.5``````

#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.