Search code examples
pythonturtle-graphicspython-turtle

'Turtle' object has no attribute 'done' as well as code freezes


I came across an exercise, and I cannot figure out how to do it.

The exercise:

Write a function named Triangle1 that takes three integers as input variables, and prints either “Yes” or “No,” depending on whether you can, or cannot, form a triangle from sides with the given lengths. In addition, I need to, "form the triangle using python turtle with the turtle creating angles based on the given sides”.

This is what I've tried so far. However I constantly get errors pertaining to either a part of turtle not being defined in some way:

AttributeError: 'Turtle' object has no attribute 'done'

or having the process finish with an exit code of 0. I think it might be related to using a specific program, but I can't seem to figure out how to fix this problem.

from math import *
import turtle


def draw_triangle(board, angles, sides):
  A, B, C = angles
  a, b, c = sides
  # draw side a
  board.forward(a)
  # Change direction and draw side b
  board.left(180-C)
  board.forward(b)
  # Change direction and draw side c
  board.left(180-A)
  board.forward(c)

  board.done()

def main():
  board = turtle.Turtle()

  a = (int(input("Enter a value for a:")))
  b = (int(input("Enter a value for b:")))
  c = (int(input("Enter a value for c:")))

  if a+b > c and b+c > a and a + c > b:
      print("Those ARE valid sides of a triangle")
  else:
      print("Those ARE NOT valid sides of a triangle")

  A = degrees(acos((a**2+b**2-c**2)/(2.0*a*b)))
  B = degrees(acos((b**2+c**2-a**2)/(2.0*b*c)))
  C = degrees(acos((c**2+a**2-b**2)/(2.0*a*c)))

  draw_triangle(board, [A, B, C], [a, b, c])

main()

I expect the output of the code to draw a triangle that changes angles based on the user inputted side lengths.


Solution

  • This revision of the code should fix the OP's stated problems as well as the trigonometry problem (i.e. the triangles should close.) It will also use previously unused angle B to return the turtle cursor to its original heading:

    from math import pi, acos
    from turtle import Turtle, mainloop
    
    def draw_triangle(board, angles, sides):
        A, B, C = angles
        a, b, c = sides
    
        # draw side a
        board.forward(a)
    
        # Change direction and draw side b
        board.left(pi - C)
        board.forward(b)
    
        # Change direction and draw side c
        board.left(pi - A)
        board.forward(c)
    
        # If all's correct, return to starting angle
        board.left(pi - B)
    
    def main():
        a = int(input("Enter a value for a: "))
        b = int(input("Enter a value for b: "))
        c = int(input("Enter a value for c: "))
    
        if a + b > c and b + c > a and a + c > b:
            print("Those ARE valid sides of a triangle")
        else:
            print("Those ARE NOT valid sides of a triangle")
    
        A = acos((b**2 + c**2 - a**2) / (2.0 * b * c))
        B = acos((c**2 + a**2 - b**2) / (2.0 * c * a))
        C = acos((a**2 + b**2 - c**2) / (2.0 * a * b))
    
        board = Turtle()
        board.radians()
    
        draw_triangle(board, [A, B, C], [a, b, c])
    
        mainloop()
    
    main()
    

    The board.done() was invalid as board is a turtle an done() is a screen method. The trigonometry problem was that the three angle calculations were assigned incorrectly.

    Finally, I switched the turtle to radians so that all the math can be done without the conversions to degrees.