Python has the same set of boolean operators you’re used to
Exceptions:
a < b < c is run as a < b and b < c
you can do some weird stuff with this:
a == b < c > d != q
a == b and b < c and c > d and d != q
if statementsif, elif and else statementsif .. elif .. elseelif not “else if”Write a Python function named classify_triangle in a file called triangle.py.
What it does: Given the lengths of three sides, decide what kind of triangle they form (if any). Parameters (in this order):
Returns: a string — one of “invalid”, “equilateral”, “isosceles”, or “scalene”.
Test cases:
assert classify_triangle(5, 5, 5) == "equilateral"
assert classify_triangle(5, 5, 8) == "isosceles"
assert classify_triangle(3, 4, 5) == "scalene"
assert classify_triangle(1, 2, 10) == "invalid" # 1 + 2 is not more than 10
assert classify_triangle(1, 2, 3) == "invalid" # 1 + 2 is not MORE than 3
assert classify_triangle(4, 0, 4) == "invalid" # zero-length side
assert classify_triangle(-1, 5, 5) == "invalid" # negative sideSubmit your solution (name your file triangle.py) to gradescope
def classify_triangle(a, b, c):
# return "invalid" if any side is zero or negative.
if a <= 0 or b <= 0 or c <= 0:
return "invalid"
# return "invalid" if the sides can't form a triangle.
if a + b <= c or a + c <= b or b + c <= a:
return "invalid"
# "equilateral" — all three sides are equal
if a == b == c:
return "equilateral"
# "isosceles" — exactly two sides are equal
if (a == b and b != c) or (a == c and b != a) or (c == b and c != a):
return "isosceles"
return "scalene"def classify_triangle(a, b, c):
# return "invalid" if any side is zero or negative.
if a <= 0 or b <= 0 or c <= 0:
return "invalid"
# return "invalid" if the sides can't form a triangle.
if a + b <= c or a + c <= b or b + c <= a:
return "invalid"
# "equilateral" — all three sides are equal
if a == b and b == c:
return "equilateral"
# "isosceles" — exactly two sides are equal
elif a == b or b == c or a == c:
return "isosceles"
else:
return "scalene"import statementsYou can import a module that already exists in base python:
Or create a .py file and import that (overwrites the standard library if you create a .py file with its name)
math.piimport math
def circle_area(r):
'''
Given a radius r, this function calculates and returns the area of that circle
Arguments:
r (integer or float) -- radius of the circle
Returns:
And float (rounded at two decimals) representing the area of the circle of radius r
'''
area = math.pi * r**2
# return rounded area at two decimals
return round(area, 2)
if __name__ == "__main__":
assert circle_area(1) == 3.14
assert circle_area(2) == 12.57
assert circle_area(3) == 28.27
assert circle_area(4) == 50.27
assert circle_area(15) == 706.86
print("Passed all tests")Passed all tests
Prompt for generative AI: Give me a list of circle radii and their corresponding area rounded to two decimals
test.py
import_test.py