Skip to content

Instantly share code, notes, and snippets.

@miquelvir
Created March 16, 2025 22:06
Show Gist options
  • Save miquelvir/f85801203bd12f244417e5788458f5ef to your computer and use it in GitHub Desktop.
Save miquelvir/f85801203bd12f244417e5788458f5ef to your computer and use it in GitHub Desktop.
devQuest - 16/03/2025 23:06

Exercise 1

Write a program that takes three side lengths as input and determines whether they form a valid triangle. If they do, determine if the triangle is equilateral, isosceles, or scalene.

Teacher solution

side1 = float(input("Side 1: "))
side2 = float(input("Side 2: "))
side3 = float(input("Side 3: "))

if side1 < side2 + side3 and side2 < side1 + side3 and side3 < side1 + side2:  # triangle inequality theorem
# (any side of a triangle must be less than the sum of the other sides)
    print("It is a triangle")
    if side1 == side2 == side3:
        print("Equilateral Triangle")
    elif side1 != side2 and side1 != side3 and side2 != side3:
        print("Scalene Triangle")
    else:
        print("Isosceles Triangle")
else:
    print("It is not a triangle")

Test cases

These test cases have been created with AI, cross check anything important.

Scalene triangle

Input

3
4
5```

#### Expected output

It is a triangle Scalene Triangle```

Equilateral triangle

Input

5
5
5```

#### Expected output

It is a triangle Equilateral Triangle```

Isosceles triangle

Input

5
5
7```

#### Expected output

It is a triangle Isosceles Triangle```

Not a triangle

Input

1
2
5```

#### Expected output

It is not a triangle```

Explanation

This explanation has been created with AI, cross check anything important.

The code first prompts the user to input the lengths of three sides. input() function takes the prompt message and reads input from the user. float() converts the input to a floating-point number, allowing for decimal values.

side1 = float(input("Side 1: "))
side2 = float(input("Side 2: "))
side3 = float(input("Side 3: "))

The code then checks if the given sides can form a triangle using the triangle inequality theorem. This theorem states that the sum of any two sides of a triangle must be greater than the third side. The if condition checks this for all three possible combinations of sides.

if side1 < side2 + side3 and side2 < side1 + side3 and side3 < side1 + side2:

If the sides can form a triangle, the code prints "It is a triangle" and then proceeds to classify the triangle based on its side lengths.

print("It is a triangle")

If all three sides are equal, it's an equilateral triangle, and the code prints "Equilateral Triangle".

if side1 == side2 == side3:
    print("Equilateral Triangle")

If all three sides are different, it's a scalene triangle, and the code prints "Scalene Triangle".

elif side1 != side2 and side1 != side3 and side2 != side3:
    print("Scalene Triangle")

If none of the above conditions are met, it means two sides are equal (but not all three), making it an isosceles triangle, and the code prints "Isosceles Triangle".

else:
    print("Isosceles Triangle")

If the initial if condition (triangle inequality theorem) is false, it means the sides cannot form a triangle, and the code prints "It is not a triangle".

else:
    print("It is not a triangle")

Practice exercises

These exercises have been created with AI, cross check anything important.

  • similar Write a program that takes three angles (in degrees) as input and determines if they form a valid triangle. If valid, determine if it's acute, right, or obtuse.
  • moderate Write a program that takes the coordinates of three points (x1, y1), (x2, y2), and (x3, y3) as input. Determine if these points form a triangle. If so, determine the type of triangle (equilateral, isosceles, or scalene) based on the distances between the points.
  • challenge Write a program that accepts three positive numbers representing the sides of a triangle. Determine if a valid triangle can be formed. If so, calculate the area of the triangle using Heron's formula and determine the type of triangle (equilateral, isosceles, or scalene). Additionally, check if the triangle is a right triangle and output the result.
  • challenge Write a program that takes the lengths of three sides as input. If a valid triangle can be formed, calculate all three angles of the triangle using the Law of Cosines. Output the angles in degrees. Additionally, determine the type of triangle (acute, right, or obtuse) based on the largest angle.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment