# how to write a function in python 3 that reads a txt file which includes three number (each line has one number) assuming these numbers are the side lengths of a triangle. The function checks the validity of the triangle and prints whether the triangle is valid or not. If is is valid, the result should also indicate the type of triangle.

Question
6 views

how to write a function in python 3 that reads a txt file which includes three number (each line has one number) assuming these numbers are the side lengths of a triangle. The function checks the validity of the triangle and prints whether the triangle is valid or not. If is is valid, the result should also indicate the type of triangle.

check_circle

star
star
star
star
star
1 Rating
Step 1

Algorithim

Start

Open a file in read mode

Read a line from file and store the number in variable a

Read a line from file and store the number in variable b

Read a line from file and store the number in variable c

if (a + b > c) and (a + c > b) and (b + c > a)

then print "Triangle is valid"

if (a==b) and (b==c)
then print "Equilateral Triangle"
else if (a!=b) and (b!=c) and (a!=c)
then print "Scalene Triangle"
else
print "Isoceles Triangle"

else print "Triangle is NOT valid"

Stop

Step 2

Python code

inputFile = open("numbers.txt", "r")
if (a + b > c) and (a + c > b) and (b + c > a) :
print("Triangle is valid")
if (a==b) and (b==c):
print("Equilateral Triangle")
elif (a!=b) and (b!=c) and (a!=c):
print("Scalene Triangle")
else:
print("Isoceles Triangle")
else:
print("Triangle is NOT valid")

Step 3

Input File

File name: numbers.txt

7
10
5

...

### Want to see the full answer?

See Solution

#### Want to see this answer and more?

Solutions are written by subject experts who are available 24/7. Questions are typically answered within 1 hour.*

See Solution
*Response times may vary by subject and question.
Tagged in