2017-02-25 58 views
0

我编写了一个程序,根据输入文件“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

请检查您的代码格式。打开输入文件的部分不会显示为代码(我认为您需要将所有4个字符进一步缩进)。 – tavnab

+0

请确保您的问题中的代码格式正确。如果我看不到代码是什么,我无法提供帮助。 – tavnab

回答

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

我刚刚意识到我的程序只给出输入文件中最后一行的输出,而不是只给出第一行。 – Bugaboo