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?
請檢查您的代碼格式。打開輸入文件的部分不會顯示爲代碼(我認爲您需要將所有4個字符進一步縮進)。 – tavnab
請確保您的問題中的代碼格式正確。如果我看不到代碼是什麼,我無法提供幫助。 – tavnab