TypeError: unsupported operand type(s) for /: 'float' and 'str'

Viewed 39

I'm trying to calculate area based on length and side number. I want to set shape number equal to 3, 4, etc. based on the shape input; essentially a convert string into an int and use it in an equation. Can someone please explain why this error is happening?

import math

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 to side number
if shape1 == 'triangle' or 'Triangle':
    shape1 == int(3)
elif shape1 == 'square' or 'Square':
    shape1 == int(4)
elif shape1 == 'pentagon' or 'Pentagon':
    shape1 == int(5)
elif shape1 == 'hexagon' or 'Hexagon':
    shape1 == int(6)
#Equate shape2 to side number
if shape2 == 'triangle' or 'Triangle':
    shape2 == int(3)
elif shape2 == 'square' or 'Square':
    shape2 == int(4)
elif shape2 == 'pentagon' or 'Pentagon':
    shape2 == int(5)
elif shape2 == 'hexagon' or 'Hexagon':
    shape2 == int(6)
#Calculate area of shape 1
apothem1 = ((0.5*int(length1))/math.tan(3.141593/(shape1)))
perimeter1 = (int(shape1)*int(length1))
area1 = (int(sides1)*int(length1)*int(apothem1))
#Calculate perimeter of shape 1
perimeter1 = (sides1*length1)
#Calculate area of shape 2
aapothem2 = ((0.5*int(length2))/math.tan(3.141593/(shape2)))
perimeter2 = (int(shape2)*int(length2))
area2 = (int(sides2)*int(length2)*int(apothem2))
#Calculate perimeter of shape 2
perimeter2 = (sides2*length2)
#Compare area values
if int(area1) > int(area2):
    print("Polygon 1's perimeter is larger")
    print(int(area1), int(area2))
elif area1 == area2:
    print("The perimeters are equal")
    print(int(area1), int(area2))
else:
    print ("Polygon 2's perimeter is larger")
    print(int(area1), int(area2))
#Compare perimeter values
if int(perimeter1) > int(perimeter2):
    print("Polygon 1's perimeter is larger")
    print(int(perimeter1), int(perimeter2))
elif int(perimeter1) == int(perimeter2):
    print("The perimeters are equal")
    print(int(perimeter1), int(perimeter2))
else:
    print ("Polygon 2's perimeter is larger")
    print(int(perimeter1), int(perimeter2))
2 Answers

You should not use == when changing a variable. Your statement should look something like:

if shape1 == 'triangle' or 'Triangle':
    shape1 = int(3)

you also redeclare perimeter 1 and 2 twice, so you might consider assigning them new variables. apothem2 is declared as aapothem2, and shape1 seems to have become sides1 at some point.

An integer is a positive or negative number that does not have a decimal, while a float is all real numbers. Where you have the method int([variable]), you're asking the program to divide a float by an integer, which gives you an error. Try replacing int([variable]) with float([variable]).

Related