Write a python program to input angles of a triangle and check whether triangle is valid or not.

Enter the First Angle of a Triangle: 50

Enter the Second Angle of a Triangle: 20

Enter the Third Angle of a Triangle: 110

This is a Valid Triangle

Last update on May 28 2022 13:44:08 (UTC/GMT +8 hours)

Write a Python program to check whether three given lengths (integers) of three sides form a right triangle. Print "Yes" if the given sides form a right triangle otherwise print "No".

Input: Integers separated by a single space.

1 ≤ length of the side ≤ 1,000

Pictorial Presentation:


Sample Solution:

Python Code:

print("Input three integers(sides of a triangle)") int_num = list(map(int,input().split())) x,y,z = sorted(int_num) if x**2+y**2==z**2: print('Yes') else: print('No')

Sample Output:

Input three integers(sides of a triangle) 8 6 7 No

Flowchart:


Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Python program to compute the digit number of sum of two given integers.
Next: Write a Python program which solve the specified equation.

What is the difficulty level of this exercise?

How to clone or copy a list?

With new_list = my_list, you don't actually have two lists. The assignment just copies the reference to the list, not the actual list, so both new_list and my_list refer to the same list after the assignment.

To actually copy the list, you have various possibilities:

  • You can use the builtin list.copy() method (available since Python 3.3):
  • new_list = old_list.copy()
  • You can slice it:
  • new_list = old_list[:]

    Alex Martelli's opinion (at least back in 2007) about this is, that it is a weird syntax and it does not make sense to use it ever. ;) (In his opinion, the next one is more readable).

  • You can use the built in list() function:
  • new_list = list(old_list)
  • You can use generic copy.copy():
  • import copy new_list = copy.copy(old_list)

    This is a little slower than list() because it has to find out the datatype of old_list first.

  • If the list contains objects and you want to copy them as well, use generic copy.deepcopy():
  • import copy new_list = copy.deepcopy(old_list)

    Obviously the slowest and most memory-needing method, but sometimes unavoidable.

Example:

import copy class Foo(object): def __init__(self, val): self.val = val def __repr__(self): return 'Foo({!r})'.format(self.val) foo = Foo(1) a = ['foo', foo] b = a.copy() c = a[:] d = list(a) e = copy.copy(a) f = copy.deepcopy(a) # edit orignal list and instance a.append('baz') foo.val = 5 print('original: %r\nlist.copy(): %r\nslice: %r\nlist(): %r\ncopy: %r\ndeepcopy: %r' % (a, b, c, d, e, f))

Output:

original: ['foo', Foo(5), 'baz'] list.copy(): ['foo', Foo(5)] slice: ['foo', Foo(5)] list(): ['foo', Foo(5)] copy: ['foo', Foo(5)] deepcopy: ['foo', Foo(1)]

Ref: //bit.ly/37iWI38

Last update on May 28 2022 13:33:56 (UTC/GMT +8 hours)

Write a Python program to check a triangle is equilateral, isosceles or scalene. Note : An equilateral triangle is a triangle in which all three sides are equal. A scalene triangle is a triangle that has three unequal sides.

An isosceles triangle is a triangle with (at least) two equal sides.

Pictorial Presentation:


Sample Solution:

Python Code:

print("Input lengths of the triangle sides: ") x = int(input("x: ")) y = int(input("y: ")) z = int(input("z: ")) if x == y == z: print("Equilateral triangle") elif x==y or y==z or z==x: print("isosceles triangle") else: print("Scalene triangle")

Sample Output:

x: 6 y: 8 z: 12 Scalene triangle

Flowchart :


Visualize Python code execution:

The following tool visualize what the computer is doing step-by-step as it executes the said program:

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Python program to check a string represent an integer or not?
Next: Write a Python program that reads two integers representing a month and day and prints the season for that month and day.

What is the difficulty level of this exercise?

How to clone or copy a list?

With new_list = my_list, you don't actually have two lists. The assignment just copies the reference to the list, not the actual list, so both new_list and my_list refer to the same list after the assignment.

To actually copy the list, you have various possibilities:

  • You can use the builtin list.copy() method (available since Python 3.3):
  • new_list = old_list.copy()
  • You can slice it:
  • new_list = old_list[:]

    Alex Martelli's opinion (at least back in 2007) about this is, that it is a weird syntax and it does not make sense to use it ever. ;) (In his opinion, the next one is more readable).

  • You can use the built in list() function:
  • new_list = list(old_list)
  • You can use generic copy.copy():
  • import copy new_list = copy.copy(old_list)

    This is a little slower than list() because it has to find out the datatype of old_list first.

  • If the list contains objects and you want to copy them as well, use generic copy.deepcopy():
  • import copy new_list = copy.deepcopy(old_list)

    Obviously the slowest and most memory-needing method, but sometimes unavoidable.

Example:

import copy class Foo(object): def __init__(self, val): self.val = val def __repr__(self): return 'Foo({!r})'.format(self.val) foo = Foo(1) a = ['foo', foo] b = a.copy() c = a[:] d = list(a) e = copy.copy(a) f = copy.deepcopy(a) # edit orignal list and instance a.append('baz') foo.val = 5 print('original: %r\nlist.copy(): %r\nslice: %r\nlist(): %r\ncopy: %r\ndeepcopy: %r' % (a, b, c, d, e, f))

Output:

original: ['foo', Foo(5), 'baz'] list.copy(): ['foo', Foo(5)] slice: ['foo', Foo(5)] list(): ['foo', Foo(5)] copy: ['foo', Foo(5)] deepcopy: ['foo', Foo(1)]

Ref: //bit.ly/37iWI38

By Srishti Chakraborti

In this tutorial, we will solve a task to check the possibility of a triangle with positive area in Python. We will take the three angles of the triangle as input. We have to see whether a triangle, with these angles, is valid or not.

To understand this code, you need to be familiar with the conditions of a possible triangle.

First, let us proceed with this, before diving deep into the problem.

Properties of Angles of a Valid Triangle

triangle, as you would all know, is a polygon with three vertices and three sides along with three external and internal angles. This polygon has a lot of properties to satisfy its validity but let us see here the ones for angles of a triangle :

  • The sum of the three interior angles has to be 180 degrees. That is, ∠a + ∠b + ∠c = 180 degrees.
  • All the values of the angles of the triangle have to be a non-zero value. If any angle is zero, the polygon will not remain a triangle.
  • The sum of two interior angles of a triangle is always greater than the third angle. That is, ∠a+∠b > ∠c  or ∠a+∠c > ∠b or ∠b+∠c > ∠a.

All these conditions have to be true for the validity of the triangle. We will be using these properties to check if the given angles can constitute a valid triangle in the given task.

Implementation of the Properties in the Task

In this task, we will use the conditions we discussed above and if the three angles fulfill the conditions of triangle validity, we will print that the triangle is valid. If not, we will print negative,i.e, the triangle is not possible. We will take the three angles as input from the user; you may also use the angle of your choice as default.

The approach is as follows :

  • First, take the three angles as input in the main code and store them in x,y,z and pass them as an argument to the function Triangle().
  • In Triangle(), run an if statement to check if the given angles have non-zero values and that the sum of the angles gives 180If both the conditions are found to be true, then proceed to check the next condition, else print “No, triangle not possible“.
  • Next, run another if statement to check the condition of whether the sum of any two angles greater than the third angle. Check this for all combinations. If any of the combinations holdthen print “Yes, triangle is valid”.
  • Else, print “No, triangle not possible“.

Python program: Possibility of Triangle with Given Angles

# function to check validity of triangle with the given angles def Triangle(x, y, z): # to check whether sum of angles are 180 and each angle # not equal to 0 if(x != 0 and y != 0 and z != 0 and (x + y + z)== 180): # Checking sum of 2 angles greater than the third if((x + y)>= z or (x + z)>= y or (y + z)>= x): print("Yes,triangle is valid") else: print("No,triangle not possible") else: print("No,triangle not possible") # Main Code x = int(input("Enter the first angle: ")) y = int(input("Enter the second angle: ")) z = int(input("Enter the third angle: ")) Triangle(x,y,z)

Conclusion

Output : Enter the first angle: 50 Enter the second angle: 60 Enter the third angle: 70 Yes,triangle is valid

Here, we took the input for three angles as 50,60,70 and see that 50+60+70 gives us 180. Also, all the values are non-zero and 50+60 > 70. Hence, all conditions are true and print “Yes, triangle is valid“.

Output : Enter the first angle: 45 Enter the second angle: 45 Enter the third angle: 80 No,triangle not possible

Here, we took the input of three angles as 45,45,80 and see that all the values are non-zero and 45+45>80. But, 45+45+80 is not equal to 180 degrees. Hence, one of the conditions fails, and “No, triangle not possible” is printed. 

Thank you for going through this article by sparing your valuable t hope this article was able to solve your doubts regarding this topic. Also, you can check out the related articles given below :

Neuester Beitrag

Stichworte