Fall 2024 - P2
Big Idea 3 | .1 | .2 | .3 | .4 | .5 | .6 | .7 | .8 | .10 |
3.7.2 Nested Conditionals Homework Hack
Student led teaching on Conditionals using Javascript and Python
Homework Hack
side1 = float(input("Side 1: "))
side2 = float(input("Side 2: "))
side3 = float(input("Side 3: "))
valid_triangle = (side1 + side2 > side3) and (side1 + side3 > side2) and (side2 + side3 > side1)
if valid_triangle:
if side1 == side2 and side2 == side3:
triangle_type = "Equilateral"
elif side1 == side2 or side1 == side3 or side2 == side3:
triangle_type = "Isosceles"
else:
triangle_type = "Scalene"
print(f"Triangle Type: {triangle_type}")
else:
print("The sides provided do not form a triangle.")
-
The code collects three sides of a triangle from the user using float(input()) to handle decimal input values.
- It checks if the three sides can form a valid triangle using the triangle inequality theorem, which ensures that the sum of any two sides must be greater than the third.
- If the sides form a valid triangle, it classifies the triangle as Equilateral (all sides equal), Isosceles (two sides equal), or Scalene (all sides different).
- The classification result is then printed using an f-string for clarity.
- If the sides don’t meet the triangle inequality condition, the code prints a message stating the sides do not form a valid triangle.