2017-02-25 3 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

私は自分のプログラムが入力ファイル内のLAST行の出力を最初に与えるのではなく、出力していることに気付きました。 – Bugaboo