def short_distance(origins,(x,y),gap):
for (i,j) in origins.spilt(“ ”):
h=[]
h.append(float(math.sqrt ((i-x)*(i-x)+(j-y)*(j-y))))
for n in h:
if not gap < n:
print 0
if gap < n :
print n
-4
A
回答
2
- 该缩进是错误的;在
for
回路应缩进比def
- 错字更多:
origins.spilt(" ")
大概应该是origins.split(" ")
2
您的代码也许是从origins
是接近(x, y)
找到点。其中有很多错误:
- 凹陷是错误的。
split()
方法拼错了。split()
方法返回扁平列表,而您期待的列表对。
前两个很容易修复。在不知道origins
字符串格式的情况下,我无法确定你想要什么。有关如何将平面列表转换为列表对的解决方案,请参见this question。
还要注意的是if
语句else
条款,所以你可以写:
if gap < n:
print n
else:
print 0
0
- 你需要导入数学
- 缩进是错误的
- 如果起源就像是一个字符串
'1,1 2,2 3,3'
,Origins.split(" ")
会给你一个字符串列表["1,1", "2,2", "3,3"]
。您需要做一些额外的工作才能将它与for循环一起使用for (i,j) in ...
您需要一个元组列表,如[(1,1),(2,2),(3,3)] math.sqrt
已经返回一个浮点数,所以你可以离开它
4
我会写更类似这样的代码。如果您运行代码,它将报告任何失败的测试(其中没有测试)。
import math
def short_distance(origins, point, gap):
"""
Describe origins, point, and gap are and what the
expected outcome is.
Then provide an example that tests the code
>>> short_distance('1,2 3,4', (5,6), 1.5)
5.65685424949
2.82842712475
"""
origins = parse_origins(origins)
distance_to_point = lambda point2: point_distance(point, point2)
# what's a better name for h?
h = map(distance_to_point, origins)
report(h, gap)
def report(h, gap):
"""
Take the results of the distances and report on them
"""
for distance in h:
if not (gap < distance):
print 0
else:
print distance
def point_distance(p1, p2):
"""
Calculate the distance between two points
>>> point_distance((0,0), (1,0))
1.0
more than one test here would be good
"""
x1, y1 = p1
x2, y2 = p2
return math.sqrt((x1-x2)**2 + (y1-y2)**2)
def parse_origins(origin_string):
"""
Parse an origins string.
>>> parse_origins('1,2 3,4')
((1.0, 2.0), (3.0, 4.0))
"""
points = origin_string.split(' ')
return tuple(map(parse_point, points))
def parse_point(point_string):
"""
Take a string like 1,2 and return a tuple of the numbers
in that string.
>>> parse_point('1,2.0')
(1.0, 2.0)
"""
return tuple(map(float, point_string.split(',')))
def test():
import doctest
doctest.testmod()
if __name__ == '__main__':
test()
0
下面的代码:
from math import sqrt
def short_distance(origins,(x,y),gap):
def distance(i, j):
ix, iy = i - x, j - y
return sqrt (ix*ix + iy*iy)
all_distances = (distance(float(i), float(j)) for (i,j) in origins)
for n in all_distances:
print (0 if gap >= n else n)
,然后用它是这样的:
>>> origin = (0, 0)
>>> points = [(1, 1), (2, 1), (1, 2), (2, 2)]
>>> gap = 1.5
>>> short_distance(points, origin, gap)
0
2.2360679775
2.2360679775
2.82842712475
我最好的猜测是这样的你想要做什么。
相关问题
- 1. 这个C++程序有什么问题?
- 2. 这个小程序有什么问题?
- 3. 这个QuickSort程序有什么问题?
- 4. 这个程序有什么问题?
- 5. 这个Clojure程序有什么问题?
- 6. 这个短程序有什么问题?
- 7. 这个程序有什么问题?
- 8. 这个C程序有什么问题?
- 9. 这个C程序有什么问题?
- 10. 这个Fortran程序有什么问题?
- 11. 这个python程序有什么问题?
- 12. 这个Android程序有什么问题?
- 13. 这个程序有什么问题?
- 14. 这个C++程序有什么问题?我什么都试过
- 15. 这个程序出了什么问题?
- 16. 这个javascript排序有什么问题
- 17. 这个SQL过程有什么问题?
- 18. 这个编程有什么问题?
- 19. 有没有人知道这个程序有什么问题?
- 20. 这个用于XML的序列化程序有什么问题?
- 21. 这个快速排序程序有什么问题?
- 22. 这个java线程程序有什么问题
- 23. 这个C程序有什么问题? (全局变量)
- 24. 这个C++程序有什么问题?它核心转储
- 25. 这个C程序有什么问题,文件读取?
- 26. 这个程序中的strcmp有什么问题?
- 27. 这个程序的逻辑有什么问题?
- 28. python中的这个旋转程序有什么问题?
- 29. 这个程序有什么问题? (字符串相关)
- 30. 这个密码重置程序有什么问题吗?
它没有文档! :P – 2009-11-15 09:27:14
哈哈。他的意思是,如果你解释你正在做的事情,以及实际发生的事情,我们会更容易地提供帮助 – 2009-11-15 09:28:14
我们将需要更多的细节。这个函数用于什么? – 2009-11-15 09:29:15