Question

In: Computer Science

You are asking to develop a “Triangle Guessing” game in Python for the assignment. The program...

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

Solutions

Expert Solution

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...


Related Solutions

Write a program using Python that allows the user to play a guessing game (please provide...
Write a program using Python that allows the user to play a guessing game (please provide a picture of code so it is easier to read). The game will choose a "secret number", a positive integer less than 10000. The user has 10 tries to guess the number. Requirements: Normally, we would have the program select a random number as the "secret number". However, for the purpose of testing your program (as well as grading it), simply use an assignment...
I need to write PYTHON program which is like a guessing game using randint function which...
I need to write PYTHON program which is like a guessing game using randint function which is already done, the user has to guess a number between 1 and 1000 and the game musn't end until you guess the number, if you input a smaller number you must get hints, the same goes if your input is bigger than the wining number , also you must get notified if you repeat a number (example, you pick 1 and in the...
Random Number Guessing Game (python) *use comments Write a program guess.py that generates a random number...
Random Number Guessing Game (python) *use comments Write a program guess.py that generates a random number in the range of 1 through 20, and asks the user to guess what the number is. If the user's guess is higher than the random number, the program should display "Too high, try again." If the user's guess is lower than the random number, the program should display "Too low, try again." If the user guesses the number, the application should congratulate the...
This is for java For my assignment, I was supposed to make a number guessing game....
This is for java For my assignment, I was supposed to make a number guessing game. The class, HiLo, should pick a random number between 1 and 100 (inclusive). Then, prompt the user to guess the number. On each guess, print if the user is correct or if the guess is too high or too low. Allow only 5 guesses. In the end, print out the correct answer and how many attempts were made. Then give the user the option...
C++ The program implements the Number Guessing Game. However, in that program, the user is given...
C++ The program implements the Number Guessing Game. However, in that program, the user is given as many tries as needed to guess the correct number. Rewrite the program so that the user has no more than five tries to guess the correct number. Your program should print an appropriate message, such as “You win!” or “You lose!”.
In this python program , you are to build a trivia game. The game should present...
In this python program , you are to build a trivia game. The game should present each question – either in order or randomly – to the player, and display up to four possible answers. The player is to input what they believe to be the correct answer.   The game will tell the player if they got it right or wrong and will display their score. If they got it right, their score will go up. If they got it...
For Python: In this assignment you are asked to write a Python program to determine the...
For Python: In this assignment you are asked to write a Python program to determine the Academic Standing of a studentbased on their CGPA. The program should do the following: Prompt the user to enter his name. Prompt the user to enter his major. Prompt the user to enter grades for 3 subjects (A, B, C, D, F). Calculate the CGPA of the student. To calculate CGPA use the formula: CGPA = (quality points * credit hours) / credit hours...
We are going to develop a guessing game where the user has to guess a secret...
We are going to develop a guessing game where the user has to guess a secret number. We will start from a simple task and refine our code step by step. Task is to develop a program that Generates a random integer number in the range of [1-10] as the secret number Asks for user to input an integer in the range of [1-10] Print out the secret number and a user's input. Steps Please download this sample code, using...
IN C++ Write a program to play the Card Guessing Game. Your program must give the...
IN C++ Write a program to play the Card Guessing Game. Your program must give the user the following choices: - Guess only the face value of the card. -Guess only the suit of the card. -Guess both the face value and suit of the card. Before the start of the game, create a deck of cards. Before each guess, use the function random_shuffle to randomly shuffle the deck.
In this assignment, you will develop a simple Web server in Python that is capable of...
In this assignment, you will develop a simple Web server in Python that is capable of processing only one request. Specifically, your Web server will (i) create a connection socket when contacted by a client (browser); (ii) receive the HTTP request from this connection; (iii) parse the request to determine the specific file being requested; (iv) get the requested file from the server’s file system; (v) create an HTTP response message consisting of the requested file preceded by header lines;...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT