In: Computer Science
In python of Jupiter notebook
Write a python function called trng that takes
three numbers x, y, and z, and specifies if those can form a
triangle (i.e., returns the word triangle if they can, and Not a
triangleotherwise).
Note: In order for three numbers to form a triangle sum of
any two of them must be greater than the third one
(e.g., x=1, y=2, z=4 cannot form a triangle because x+y is not
greater than z even though x+z>y and y+z>x).
No output is needed for this problem. Yet you will need to call
this function in writing your code for Problem 1.2 below.
Python code:
#defining trng function
def trng(x,y,z):
#converting them to a sorted list
lis=sorted([x,y,z])
#checking if the sum of first 2 values in the sorted list is
greater than the 3rd one
if(sum(lis[:2])>lis[-1]):
#returning trianle
return "triangle"
else:
#returning Not a trianle
return"Not a triangle"
#calling trng function and testing for a sample value which is Not
a trianle
print(trng(1,2,4))
#calling trng function and testing for a sample value which is a
trianle
print(trng(2,3,4))
Screenshot:
Output: