这里有新手。我正在尝试创建一个可以计算受控制的简单多边形区域的代码:用户输入角点的坐标,然后一旦用户输入" STOP"就会计算区域,给定他/她至少给了3个坐标;否则,会出现一条错误消息,提示他输入另一对x和y坐标,直到他至少给出3对。
当我尝试运行程序时,我总是收到此错误消息: 您的程序出错:语法无效
然后变量v以红色突出显示。
是的,这是我的代码:ans = ""
sigma2 = 0.0
corner_counter = 1
u1 = float(input("\nx-coordinate of the first corner: "))
v1 = float(input("y-coordinate of the first corner: "))
pu = u1
pv = v1
print "Number of points given: ",corner_counter # just to check the number corners given
while ans != "STOP":
ans = raw_input("Enter x-coordinate of the next corner or type 'STOP' to compute the area: ")
if ans == "STOP":
if corner_counter >= 3:
break
else:
u = float(input("ERROR: Must enter at least three (3) corners to compute the area./nEnter x-coordinate of the next corner: ")
v = float(input("Enter y-coordinate: "))
sigma2 += ((pu * v) - (pv * u))
print "(%d * %d) - (%d * %d) =" %(pu, v, pv, u), (pu * v) - (pv * u)
print "Partial sum:", sigma2 # to check if the running sum agrees with the expected result
corner_counter += 1
continue
else:
u = float(ans)
v = float(input("Enter y-coordinate: "))
sigma2 += ((pu * v) - (pv * u))
print "(%d * %d) - (%d * %d) =" %(pu, v, pv, u), (pu * v) - (pv * u)
print "Partial sum:", sigma2 # to check if the running sum agrees with the expected result
corner_counter += 1
print "Number of points given: ", corner_counter # for checking purposes
pu = u
pv = v
print "(%d * %d) - (%d * %d) =" %(pu, v1, pv, u1), (pu * v1) - (pv * u1)
sigma2 += (u * v1) - (v * u1)
print "\nTOTAL SUM:", sigma2
# final output
print "\nThe area of the", corner_counter,"-sided polygon is", (abs(sigma2)/2), "square unit(s)."
我已经尝试过更改但似乎没有任何效果。
希望你能帮助我们。非常感谢!