- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试调整我的 python 程序的主文件,但它给出的是“未绑定(bind)的本地错误:分配前引用的局部变量 'sider'”。我不确定这是如何发生的,因为变量“角度”位于同一位置但它可以工作。任何帮助表示赞赏!
from getdata import *
from trifun import *
def main():
outfile = open("SFout.txt", "w")
myList = getData()
# print(myList)
names = myList[0]
vertices = myList[1]
for i in range(len(names)):
x1 = vertices[i][0][0]
y1 = vertices[i][0][1]
x2 = vertices[i][1][0]
y2 = vertices[i][1][1]
x3 = vertices[i][2][0]
y3 = vertices[i][2][1]
print((names[i]), (x1, y1), ",", (x2, y2), ",", (x3, y3))
print((names[i]), (x1, y1), ",", (x2, y2), ",", (x3, y3), file = outfile)
if duplicatePts(vertices[i]):
print("You have duplicate points, not a triangle")
outfile.write("You have duplicate points, not a triangle \n \n")
print("\n")
continue
if collinear(vertices[i]):
print("The points are collinear, not a triangle")
outfile.write("\nThe points are collinear, not a triangle \n \n")
print("\n")
continue
v = findAllSides(vertices[i])
pm = str("%.1f" % (perimeter(vertices[i])))
print("Perimeter: ", pm.ljust(25), end="")
print("Perimeter: ", pm.ljust(25), end="", file = outfile)
s = ("Sidelengths %0.2f, %0.2f, %0.2f" % (v[0], v[1], v[2]))
sidelength1 = str("%.1f" % (v[0]))
sidelength2 = str("%.1f" % (v[1]))
sidelength3 = str("%.1f" % (v[2]))
side = sidelength1 + "," + sidelength2 + "," + sidelength3
print("Side lengths: ", side)
print("Side lengths: ", side, file=outfile)
#outfile.write("Side length:" + side + '\n')
if acute(vertices[i]):
angle = "Acute"
# print("Acute")
# outfile.write("\nAcute")
elif right(vertices[i]):
angle = "Right"
# print("Right")
# outfile.write("\nRight")
elif obtuse(vertices[i]):
angle = "Obtuse"
# print("Obtuse")
# outfile.write("\nObtuse")
if Scalene(vertices[i]):
side = "Scalene"
# print("Scalene")
# outfile.write("\nScalene")
elif Equilateral(vertices[i]):
side = "Equilateral"
# print("Equilateral")
# outfile.write("\nEquilateral")
elif Isosceles(vertices[i]):
side = "Isosceles"
# print("Isosceles")
# outfile.write("\nIsosceles")
# f.close()
print((angle + " & " + side).ljust(37), end="")
print((angle + " & " + side).ljust(37), end="", file=outfile)
#outfile.write( angle + " & " + side+"\n")
print("Area is: %0.2f" % area(vertices[i]))
print("Area is: %0.2f" % area(vertices[i]), "\n", file=outfile)
#outfile.write("Area is: %0.2f" % area(vertices[i])+"\n")
print("\n")
outfile.close()
main()
def getData():
names = []
vertices = []
myList = [names,vertices]
with open("test.txt") as f:
for line in f:
x = line.split()
names.append(x[0])
x1 = float(x[1])
y1 = float(x[2])
x2 = float(x[3])
y2 = float(x[4])
x3 = float(x[5])
y3 = float(x[6])
vertices.append([[x1,y1],[x2,y2],[x3,y3]])
return myList
# The text file contains vertices of three points of a triangle, separated by a space.
# Import Math for calculations.
import math
# This function checks if there are any duplicate points in te vertices of the triangle.
# If there are, it s not a triangle.
def duplicatePts(vertices):
#vertices looks like this : [[x1,y1],[x2,y2],[x3,y3]]
x1 = vertices[0][0]
y1 = vertices[0][1]
x2 = vertices[1][0]
y2 = vertices[1][1]
x3 = vertices[2][0]
y3 = vertices[2][1]
if (x1,y1)==(x2,y2) or (x2, y2) == (x3, y3) or (x1, y1) == (x3, y3):
return True
else:
return False
# This function checks if the points of the triangle are collinear.
# Triangle points cannot be in the same line. They have to be in different positions on the lane.
def collinear(vertices):
x1 = vertices[0][0]
y1 = vertices[0][1]
x2 = vertices[1][0]
y2 = vertices[1][1]
x3 = vertices[2][0]
y3 = vertices[2][1]
if (x2 - x1) == 0:
# As discussed in class, set it to a high number, to assume it is vertical
slope1 = 9999
else:
slope1 = (y2 - y1) / (x2 - x1)
if (x3 - x2) == 0:
# As discussed in class, set it to a high number, to assume it is vertical
slope2 = 9999
else:
slope2 = (y3 - y2) / (x3 - x2)
if slope1 == slope2:
return True
else:
return False
# To find the perimeter, we need to find the sides first.
# Perimeter = Sum of all three sides
def perimeter(vertices):
x1 = vertices[0][0]
y1 = vertices[0][1]
x2 = vertices[1][0]
y2 = vertices[1][1]
x3 = vertices[2][0]
y3 = vertices[2][1]
sideAB = sideLength(x1, y1, x2, y2)
sideBC = sideLength(x2, y2, x3, y3)
sideAC = sideLength(x3, y3, x1, y1)
perimeter = sideAB + sideAC + sideBC
return perimeter
# Finding the length of the side using the distance formula.
def sideLength(x1, y1, x2, y2):
length = math.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2)
return length
# Using the min function to find out the shortest side.
def findAllSides(vertices):
x1 = vertices[0][0]
y1 = vertices[0][1]
x2 = vertices[1][0]
y2 = vertices[1][1]
x3 = vertices[2][0]
y3 = vertices[2][1]
sideAB = sideLength(x1, y1, x2, y2)
sideBC = sideLength(x2, y2, x3, y3)
sideAC = sideLength(x3, y3, x1, y1)
lst=[sideAB, sideAC, sideBC]
return lst
# We know how to find the sides now.
# We determine if the triangle is a right triangle by using Pythagoras theorem.
# Pythagoras theorem states that a triangle is a right triangle when
# Square of Hypotenus is equal to the sum of other two sides.
def typeObtuseRightAcute(vertices):
#no idea if this is a good value but works for example
#and should be low enough to give right answers for all but crazy close triangles
epsilon=10**-8
x1 = vertices[0][0]
y1 = vertices[0][1]
x2 = vertices[1][0]
y2 = vertices[1][1]
x3 = vertices[2][0]
y3 = vertices[2][1]
# Using Pythagoras theorem
sideAB = sideLength(x1, y1, x2, y2)
sideBC = sideLength(x2, y2, x3, y3)
sideAC = sideLength(x3, y3, x1, y1)
#use this instead
[var1,var2,largest] = sorted([sideAB, sideBC, sideAC])
if abs((largest) ** 2-((var1 ** 2 + (var2) ** 2)))<epsilon:
return "right"
elif (largest) ** 2 > ((var1 ** 2 + (var2) ** 2)):
return "obtuse"
else:
return "acute"
def acute(vertices):
return typeObtuseRightAcute(vertices)=="acute"
def right(vertices):
return typeObtuseRightAcute(vertices)=="right"
def obtuse(vertices):
return typeObtuseRightAcute(vertices)=="obtuse"
# A triangle is a scalene triangle, when all three sides are not equal to each other.
def Scalene(vertices):
EPSILON = 0.000001
x1 = vertices[0][0]
y1 = vertices[0][1]
x2 = vertices[1][0]
y2 = vertices[1][1]
x3 = vertices[2][0]
y3 = vertices[2][1]
sideAB = sideLength(x1, y1, x2, y2)
sideBC = sideLength(x2, y2, x3, y3)
sideAC = sideLength(x3, y3, x1, y1)
if (abs(sideAB-sideBC)>=EPSILON and abs(sideBC-sideAC)>=EPSILON and abs(sideAB-sideAC)>=EPSILON):
return True
# A triangle is an isosceles triangle when 2 of the sides are equal.
def Isosceles(vertices):
EPSILON = 0.000001
x1 = vertices[0][0]
y1 = vertices[0][1]
x2 = vertices[1][0]
y2 = vertices[1][1]
x3 = vertices[2][0]
y3 = vertices[2][1]
sideAB = sideLength(x1, y1, x2, y2)
sideBC = sideLength(x2, y2, x3, y3)
sideAC = sideLength(x3, y3, x1, y1)
if (abs(sideAB-sideBC)<EPSILON or abs(sideBC-sideAC)<EPSILON or abs(sideAB-sideAC)<EPSILON) and not (abs(sideAB-sideBC)<EPSILON and abs(sideBC-sideAC)<EPSILON and abs(sideAB-sideAC)<EPSILON):
return True # or True
else:
return False
# A triangle is an equilateral triangle when all three sides are equal to each other.
def Equilateral(vertices):
EPSILON = 0.000001
x1 = vertices[0][0]
y1 = vertices[0][1]
x2 = vertices[1][0]
y2 = vertices[1][1]
x3 = vertices[2][0]
y3 = vertices[2][1]
sideAB = sideLength(x1, y1, x2, y2)
sideBC = sideLength(x2, y2, x3, y3)
sideAC = sideLength(x3, y3, x1, y1)
if (abs(sideAB-sideBC)<EPSILON and abs(sideBC-sideAC)<EPSILON and abs(sideAB-sideAC)<EPSILON):
return True
else:
return False
# To find the area, we need to find the semi perimeter first.
# We find the area of triangle using Heron's formula.
def area(vertices):
x1 = vertices[0][0]
y1 = vertices[0][1]
x2 = vertices[1][0]
y2 = vertices[1][1]
x3 = vertices[2][0]
y3 = vertices[2][1]
sideAB = sideLength(x1, y1, x2, y2)
sideBC = sideLength(x2, y2, x3, y3)
sideAC = sideLength(x3, y3, x1, y1)
semiPerimeter = (sideAB + sideBC + sideAC) / 2
area = (semiPerimeter * (semiPerimeter - sideAB) * (semiPerimeter - sideBC) * (semiPerimeter - sideAC)) ** 0.5
return area
Alpha -4 0 4 0 0 6.9282
Bravo -2.161 -3.366 2.161 3.366 -5.83 3.743
Charlie 3.54 5.46 -4.54 5.557 3.1 -2.1
Delta 0 4 0 2 0 2
Echo -4 3 1 3 6 3
Foxtrot 0 0 5 1 10 0
Golf 0 4 0 0 4 0
最佳答案
我不确定这是否能让您满意,但这会阻止获得 Unbound Local Error
:
sider = ("Scalene"
if scalene(vertices[i])
else ("Equilateral"
if equilateral(vertices[i])
else ("Isosceles"
if isosceles(vertices[i])
else "None of the above")
)
)
sider
:
def main():
outfile = open("SFout.txt", "w")
myList = getData()
names = myList[0]
vertices = myList[1]
sider = '' # declare this one
angle = '' # and this one
for i in range(len(names)):
x1 = vertices[i][0][0]
y1 = vertices[i][0][1]
x2 = vertices[i][1][0]
y2 = vertices[i][1][1]
x3 = vertices[i][2][0]
y3 = vertices[i][2][1]
print((names[i]), (x1, y1), ",", (x2, y2), ",", (x3, y3))
print((names[i]), (x1, y1), ",", (x2, y2), ",", (x3, y3), file=outfile)
....
关于python - Python中的未绑定(bind)本地错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36097021/
我已经使用 vue-cli 两个星期了,直到今天一切正常。我在本地建立这个项目。 https://drive.google.com/open?id=0BwGw1zyyKjW7S3RYWXRaX24tQ
您好,我正在尝试使用 python 库 pytesseract 从图像中提取文本。请找到代码: from PIL import Image from pytesseract import image_
我的错误 /usr/bin/ld: errno: TLS definition in /lib/libc.so.6 section .tbss mismatches non-TLS reference
我已经训练了一个模型,我正在尝试使用 predict函数但它返回以下错误。 Error in contrasts<-(*tmp*, value = contr.funs[1 + isOF[nn]])
根据Microsoft DataConnectors的信息我想通过 this ODBC driver 创建一个从 PowerBi 到 PostgreSQL 的连接器使用直接查询。我重用了 Micros
我已经为 SoundManagement 创建了一个包,其中有一个扩展 MediaPlayer 的类。我希望全局控制这个变量。这是我的代码: package soundmanagement; impo
我在Heroku上部署了一个应用程序。我正在使用免费服务。 我经常收到以下错误消息。 PG::Error: ERROR: out of memory 如果刷新浏览器,就可以了。但是随后,它又随机发生
我正在运行 LAMP 服务器,这个 .htaccess 给我一个 500 错误。其作用是过滤关键字并重定向到相应的域名。 Options +FollowSymLinks RewriteEngine
我有两个驱动器 A 和 B。使用 python 脚本,我在“A”驱动器中创建一些文件,并运行 powerscript,该脚本以 1 秒的间隔将驱动器 A 中的所有文件复制到驱动器 B。 我在 powe
下面的函数一直返回这个错误信息。我认为可能是 double_precision 字段类型导致了这种情况,我尝试使用 CAST,但要么不是这样,要么我没有做对...帮助? 这是错误: ERROR: i
这个问题已经有答案了: Syntax error due to using a reserved word as a table or column name in MySQL (1 个回答) 已关闭
我的数据库有这个小问题。 我创建了一个表“articoli”,其中包含商品的品牌、型号和价格。 每篇文章都由一个 id (ID_ARTICOLO)` 定义,它是一个自动递增字段。 好吧,现在当我尝试插
我是新来的。我目前正在 DeVry 在线学习中级 C++ 编程。我们正在使用 C++ Primer Plus 这本书,到目前为止我一直做得很好。我的老师最近向我们扔了一个曲线球。我目前的任务是这样的:
这个问题在这里已经有了答案: What is an undefined reference/unresolved external symbol error and how do I fix it?
我的网站中有一段代码有问题;此错误仅发生在 Internet Explorer 7 中。 我没有在这里发布我所有的 HTML/CSS 标记,而是发布了网站的一个版本 here . 如您所见,我在列中有
如果尝试在 USB 设备上构建 node.js 应用程序时在我的树莓派上使用 npm 时遇到一些问题。 package.json 看起来像这样: { "name" : "node-todo",
在 Python 中,您有 None单例,在某些情况下表现得很奇怪: >>> a = None >>> type(a) >>> isinstance(a,None) Traceback (most
这是我的 build.gradle (Module:app) 文件: apply plugin: 'com.android.application' android { compileSdkV
我是 android 的新手,我的项目刚才编译和运行正常,但在我尝试实现抽屉导航后,它给了我这个错误 FAILURE: Build failed with an exception. What wen
谁能解释一下?我想我正在做一些非常愚蠢的事情,并且急切地等待着启蒙。 我得到这个输出: phpversion() == 7.2.25-1+0~20191128.32+debian8~1.gbp108
我是一名优秀的程序员,十分优秀!