Python Incorrect Output Using float()

Viewed 28

I'm trying to calculate a shapes area and perimeter and then compare them. If, for example, I wanted to get the area of a square with length = 5, then I'd expect the area = length * length = 5 * 5 = 25. However, I'm getting completely different answers and am not sure why. What is wrong with my calculations in Python?

import math
pi = 3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651328230664709384460955058223172535940812848111745028410270193852110555964462294895

shape1 = input('What is your first shape? ')
length1 = input("What is the length of its side? ")
shape2 = input("What is your second shape? ")
length2 = input("What is the length of its side? ")

#Equate shape1 area and perimeter
if shape1 == 'Triangle' or 'triangle':
    shape1 = float(3)
    area1 = ((math.sqrt(3)/float(4))*float(length1)*float(length1))
    perimeter1 = float(length1) * float(3)
elif shape1 == 'Square' or 'square':
    shape1 = float(4)
    area1= float(length1) * float(length1)
    perimeter1 = float(length1)*4
elif shape1 == 'Hexagon' or 'hexagon':
    shape1 = float(6)
    area1 = (((3*(math.sqrt(3)))/2)*length1*length1)
    perimeter1 = float(length1) * float(6)
#Equate shape2 area and perimeter
if shape2 == 'Triangle' or 'triangle':
    shape2 = float(3)
    area2 = ((math.sqrt(3)/float(4))*float(length2)*float(length2))
    perimeter2 = float(length2) * float(3)
elif shape2 == 'Square' or 'square':
    shape2 = float(4)
    area2 = float(length2) * float(length2)
    perimeter2 = float(length2) * float(4)
elif shape2 == 'Hexagon' or 'hexagon':
    shape2 = float(6)
    area2 = (((3*(math.sqrt(3)))/float(2))*float(length2)*float(length2))
    perimeter2 = float(length2) * float(6)

#Compare area values and print
if float(area1) > float(area2):
    print("Polygon 1's area is larger")
    print(float(area1), float(area2))
elif area1 == area2:
    print("The areas are equal")
    print(float(area1), float(area2))
else:
    print ("Polygon 2's area is larger")
    print(float(area1), float(area2))

#Compare perimeter values and print
if float(perimeter1) > float(perimeter2):
    print("Polygon 1's perimeter is larger")
    print(float(perimeter1), float(perimeter2))
elif float(perimeter1) == float(perimeter2):
    print("The perimeters are equal")
    print(float(perimeter1), float(perimeter2))
else:
    print ("Polygon 2's perimeter is larger")
    print(float(perimeter1), float(perimeter2))

Example output

What is your first shape? square
What is the length of its side? 5.5
What is your second shape? triangle
What is the length of its side? 3
Polygon 1's area is larger
13.098634232239634 3.897114317029974
Polygon 1's perimeter is larger
16.5 9.0

Process finished with exit code 0
1 Answers
if shape1 == 'Triangle' or 'triangle':

The trouble is that this is parsed as (shape1 == 'Triangle') or ('triangle'), and then the string 'triangle' is not empty, so it is considered true, and so the whole if statement is true.

This should be

if shape1 == 'Triangle' or shape1 == 'triangle':

or better yet use a set,

if shape1 in {'Triangle', 'triangle'}:

or even better:

if shape1.casefold() == 'triangle':

See also How to test multiple variables for equality against a single value?

Related