无法在输入文件的第一行输出输出

时间:2017-02-25 00:30:08

标签: python python-3.x input

我编写了一个程序,用于根据输入文件“RCOut04.txt”中给出的坐标对三角形进行分类。但是它只是从最后一行输入打印输出。

如何修复此问题,以便为每行输入打印输出?

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?

1 个答案:

答案 0 :(得分:0)

您需要先将输入分为行(请参阅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...