In: Computer Science
You are asking to develop a “Triangle Guessing” game in Python for the assignment. The program accepts the lengths of three (3) sides of a triangle as input from a player. The program output should indicate whether the triangle is a right triangle, an acute triangle, or an obtuse triangle.
1.Tips to help you for this assignment: Make sure each side of the triangle is a positive integer. Use try/except for none positive integer input.
2. Validating the triangle. That is the sum of the lengths of any two sides of a triangle is greater than the length of the third side. Use try/except for invalid sides input.
3. For any wrong input, tell the player what is wrong and asking to re-enter.
4. Allow a player to play multiple times, for example, using a while loop. Remind the player what input to stop the game.
5. Develop your game/program a user friendly one. Document your program by adding comments: Introduce/describe the program including the author, date, goal/purpose of the program Document every variable used at the program even the name is meaningful Explain/comment operation/activity/logic of your code. For example, “Checking whether the inputs are positive integers” and “Checking the validity the 3 lengths to see if they are able to form a triangle”
6. Testing your program for all possible input and every of the 3 possible triangles
Screenshot
----------------------------------------------------------------------------------------
Program
'''
Program to find the type of triangle
Accept only positive integers as sides of
triangle
Check valid triangle, two sides always greater than
3rd side
Find largest side from 3 sides
Use pythogorean concept to find type of triangle
'''
#Function treturn largest side from given 3 sides
def findLarge(side1,side2,side3):
if side1>side2 and side1>side3:
return side1
elif side2>side1 and side2>side3:
return side2
else:
return side3
#Function print the type of triangle
def findTriangleType(side1,side2,side3):
largest=findLarge(side1,side2,side3)
sum=0
if(side1<largest):
sum+=side1*side1
if(side2<largest):
sum+=side2*side2
if(side3<largest):
sum+=side3*side3
#If 2 sides squared sum greater than large side
squared then accute
if(sum>(largest*largest)):
print('Given triangle is
acute triangle!!!')
#If 2 sides squared sum less than large side
squared then obtuse
elif(sum<(largest*largest)):
print('Given triangle is
obtuse triangle!!!')
#If 2 sides squared sum equal to the large side
squared then right angled
else:
print('Given triangle is
right angle triangle!!!')
#Main method
def main():
#Loop unti user prefer
while(True):
try:
#Prompt for sides and error check
side1=int(input('Enter side1 length: '))
assert side1>0
side2=int(input('Enter side2 length: '))
assert side2>0
side3=int(input('Enter side3 length: '))
assert side3>0
#Valid side check
if(side1+side2<=side3 or side1+side2<=side2 or
side2+side3<side1):
raise ValueError('Error!!Sum of 2 sides of a triangle must be
greater than third side!!')
#Call function
findTriangleType(side1,side2,side3)
#Repetition
ch=input('Do you want to check other triangle(y/n): ')
while(ch!='y' and ch!='Y' and ch!='n' and ch!='N'):
print('ERROR!!Please enter y or n')
ch=input('Do you want to check other triangle(y/n): ')
if ch=='n'or ch=='N':
print('Ending the program...')
break
#Error catching
except
AssertionError:
print("Oops! That was not a positive integer number. Try
again...")
except ValueError as
e:
print(e)
main()
--------------------------------------------------------------------------------------------------------------------
Output
Enter side1 length: -5
Oops! That was not a positive integer number. Try again...
Enter side1 length: 1
Enter side2 length: 10
Enter side3 length: 12
Error!!Sum of 2 sides of a triangle must be greater than third
side!!
Enter side1 length: 7
Enter side2 length: 10
Enter side3 length: 5
Given triangle is obtuse triangle!!!
Do you want to check other triangle(y/n): t
ERROR!!Please enter y or n
Do you want to check other triangle(y/n): y
Enter side1 length: 8
Enter side2 length: 17
Enter side3 length: 15
Given triangle is right angle triangle!!!
Do you want to check other triangle(y/n): y
Enter side1 length: 4
Enter side2 length: 6
Enter side3 length: 7
Given triangle is acute triangle!!!
Do you want to check other triangle(y/n): n
Ending the program...