Dhruv Sharma
Dhruv Sharma

Reputation: 37

Keep on getting TypeError: unsupported operand type(s) for *=: 'function' and 'float' error for pythagorean triplet

I have been trying to make a code which deduces whether a right-angled triangle is a pythagorean triplet or not. I have then tried to draw the triangle in turtle. However, when I try and get the degrees between the base and the hypotenuse, I get this:

TypeError: unsupported operand type(s) for *=: 'function' and 'float'

This is my code:

   import math
   isrightangled = int(input('is your triangle right-angled? enter 1 for no. if it is, press any other number'))
   if isrightangled == 1:
       print('your triangle is not a pythagorean triplet.')
   else:
       a = int(input('Please enter the perpendicular height of your triangle.'))
       b = int(input('please enter the base length of your triangle.'))
       apowerof2 = a * a
       bpowerof2 = b * b
       cpowerof2 = apowerof2 + bpowerof2
       c = math.sqrt(cpowerof2)
       degrees = int(math.degrees(math.atan(a/b)))
       print(degrees)
       cinput = int(input('Please enter the length of the hypotenuse of the triangle.'))
       if c == cinput:
           print ('your triangle is a pythagorean triplet')
           from turtle import *
           drawing_area = Screen()
           drawing_area.setup(width=750, height=900)
           shape('circle')
           left(90)
           forward(a * 100)
           backward(a*100)
           right(90)
           forward(b*100)
           left(degrees)
           forward(c*100)
           done()
       else:
           print ('your triangle is not a pythagorean triplet.')

Any help would be greatly appreciated!

Upvotes: 2

Views: 227

Answers (1)

atanay
atanay

Reputation: 174

The problem is the name of the degrees variable, it collides with the turtle.degrees() function. (https://www.geeksforgeeks.org/turtle-degrees-function-in-python/)

Just change the variable name

Also, here are two notes:

  • there's no need to create that many variables in order to calculate c. you can just write c = math.sqrt(a * a + b * b)
  • left(degrees) is not enough, you need to do left(180-degrees)

here's the code after these changes:

import math
from turtle import *
isrightangled = int(input('is your triangle right-angled? enter 1 for no. if it is, press any other number'))
if isrightangled == 1:
    print('your triangle is not a pythagorean triplet.')
else:
    a = int(input('Please enter the perpendicular height of your triangle.'))
    b = int(input('please enter the base length of your triangle.'))
    c = math.sqrt(a * a + b * b)
    degs = int(math.degrees(math.atan(a/b)))
    cinput = int(input('Please enter the length of the hypotenuse of the triangle.'))
    if c == cinput:
        print ('your triangle is a pythagorean triplet')
        drawing_area = Screen()
        drawing_area.setup(width=750, height=900)
        shape('circle')
        left(90)
        forward(a * 100)
        backward(a*100)
        right(90)
        forward(b*100)
        left(180-degs)
        forward(c*100)
        done()
    else:
        print ('your triangle is not a pythagorean triplet.')

Upvotes: 1

Related Questions