0

I have written a program to classify triangles from co-ordinates given in an input file "RCOut04.txt". However is it only printing output from the last line of input.

How do I fix this so it prints the output for every line of input?

file = open("RCOut04.txt", "w")
with open("input4.txt") as f:
    for line in f:
        datastr = line.split()
        data = [float(x) for x in datastr]

        # Checking if it is a triangle
    if duplicatePts(data[0], data[1], data[2], data[3], data[4], data[5]) == True or collinear(data[0], data[1],
                                                                                                   data[2], data[3],
                                                                                                   data[4],
                                                                                                   data[5]) == True:
            with open("RCOut04.txt", "w") as file:
                file.write("It is not triangle")
            #If not a triangle
            print("It is not triangle")

    else:
            #write vertices
            file.write(
                "Vertices: ( %f, %f ) , (%f ,%f), (%f, %f) " % (data[0], data[1], data[2], data[3], data[4], data[5]))

            # Print vertices and shortest side
            print("Vertices:", "( ", data[0], " ,", data[1], "), ", "( ", data[2], ", ", data[3], "), ", "( ", data[4], ",", data[5],
                  ")", "    Shortest Side is:",findShortest(data[0], data[1], data[2], data[3], data[4], data[5]))


            # write shortest side
            file.write("Shortest Side is: %f " % findShortest(data[0], data[1], data[2], data[3], data[4], data[5]))



            # write perimeter
            file.write("Perimeter is: %f " % perimeter(data[0], data[1], data[2], data[3], data[4], data[5]))

            #Print perimeter and area on same side
            print("Perimeter is:", perimeter(data[0], data[1], data[2], data[3], data[4], data[5]), "       Area is:", area(data[0], data[1], data[2], data[3], data[4], data[5]))


            # write area
            file.write("Area is: %f " % area(data[0], data[1], data[2], data[3], data[4], data[5]))



            # check if Right triangle
            if (right(data[0], data[1], data[2], data[3], data[4], data[5])) == True:

                file.write("Right Angled")
                print("Right Angled")

            # Chek for acute triangle
            if acute(data[0], data[1], data[2], data[3], data[4], data[5]) == True:

                file.write(" Acute")
                print("Acute")

            # Check if Obtuse
            if obtuse(data[0], data[1], data[2], data[3], data[4], data[5]) == True:

                file.write(" Obtuse")

                print("Obtuse")

            # Check for Equilateral
            if equilateral(data[0], data[1], data[2], data[3], data[4], data[5]) == True:

                file.write(" equilateral")

                print("equilateral")

            # Check for Isosceles
            if (isosceles(data[0], data[1], data[2], data[3], data[4], data[5])) == True:

                file.write(" Isosceles")

                print("Isosceles")

            # Check for scalene
            if (scalene(data[0], data[1], data[2], data[3], data[4], data[5])) == True:

                file.write(" Scalene")

                print("Scalene")
    file.closed


I need some help being able to give outputs past using the first line in the input file.  What can I do or change to the code to do this code for all lines in the input file?
Edd Saunders
  • 138
  • 7
Bugaboo
  • 21
  • 2
  • 1
    Please check your code formatting. The part that opens your input file is not appearing as code (I think you need to indent everything 4 characters further). – tavnab Feb 25 '17 at 00:35
  • Please make sure the code in your question is properly formatted. I can't help if I can't see what the code is. – tavnab Feb 25 '17 at 04:52

1 Answers1

0

You need to break your input into lines first (see this answer).

with open("input4.txt") as f:
    lines = f.readlines()

for line in lines:
    datastr = line.split()
    data = [float(x) for x in datastr]
    # the rest of your code...
Community
  • 1
  • 1
tavnab
  • 2,594
  • 1
  • 19
  • 26
  • I just realized my program is only giving the output for the LAST line in the input file instead of only giving the first. – Bugaboo Feb 25 '17 at 01:14