我一直在Bézier curve工作,我一切正常,但我想确保用户输入正确的输入。
我需要声明检查输入值是否是包含两个数字的列表,让它为整数或浮点数。欢迎提供更多信息。
如果有人需要代码,请转到此处。 (在某处可能只是一个公式,这里效率很低。^^)
#finding a point on vector based on start point, end and %
def findPoint(t, A, B):
'''
findPoint( 'float member of <0, 1>',
'coordinates of point A written as [x, y]',
'coordinates of point B written as [x, y]')
'''
x=(B[0]-A[0])*t+A[0]
y=(B[1]-A[1])*t+A[1]
return [x, y]
#find coordinates of a point on the bezier curve
def bezierCurve(t, *points):
pointList=[]
for X in points:
pointList.append(X)
while len(pointList) !=1:
tempList=[]
for i in xrange(0, len(pointList)-1):
tempList.append(findPoint(t, pointList[i], pointList[i+1]))
pointList=tempList
return pointList.pop()
答案 0 :(得分:2)
您可以使用int
float
(或all
)
>>> l = [1,2,3]
>>> a = ['a','b','c']
>>> all(isinstance(i, int) for i in l)
True
>>> all(isinstance(i, int) for i in a)
False
您也可以查看len(list) == 2
所以作为一个函数,它可能像
def twoNumValues(myList):
return len(myList) == 2 and all(isinstance(i, int) or isinstance(i, float) for i in myList)
或
def twoNumValues(myList):
return len(myList) == 2 and all(type(i) in [float, int] for i in myList)
答案 1 :(得分:0)
这是一个测试您描述的参数的单行函数:
def check(l):
return len(l) == 2 and all(type(i) in (int, float) for i in l)
首先检查长度是否正好为2,然后检查它们(全部)是int还是float。