如何检查坐标是否位于某个区域?

时间:2013-04-10 08:39:36

标签: python arrays loops coordinates

我需要创建代码,以便您检查输入的坐标是否位于某个区域内。到目前为止我有这个:

import random
import math
import pylab
import numpy    
pylab.close("all")                                                      #All import statements
x = [(random.randint(-50,50)) for i in range(10)]               #Creating list of x coordinates
y = [(random.randint(-50,50)) for j in range(10)]               #Creating list of y coordinates
array=zip(x,y)                                                          #Creating an array by combining the x and y coordinates
print array             

counter = 0                                 #Start of 1c, resetting counter
for i, j in array:                              #Telling what to inspect
        if 35**2 <= (i**2+j**2) <= 65**2:                   #Conditions for the coordinates to fall within 15 pixels of a circle with radius 50
                counter+= 1                         #If conditions met then add 1 to counter
n=(1.0*counter/7000)*100                            #Calculating percentage of coordinates in the region
print "on average", n, "% of the locations in the array fall in this region"    #Print result, end of part 1c


name = raw_input('type a coordinate location: ')                #Start of 1d, python input result
for i, j in name:
    if i in name in array:   
        if 35**2 <= (i**2+j**2) <= 65**2:
            print "warning, your chosen location falls near the edge of the circle"
    else:
         print "coordinate does not exist"

但是此刻我收到一条错误消息,说“需要超过1个值才能解包”,指的是'name = raw_input('键入一个坐标位置:')'行。我做错了什么?

2 个答案:

答案 0 :(得分:0)

name是一个字符串。你不能使用

for i, j in name:

你需要先将它分解为值,然后创建一个元组。

可能是这样的:

n =(name.split(',')[0],name.split(',')[1]) - 假设坐标用','分隔

答案 1 :(得分:0)

“raw_input”返回一个字符串。你需要拆分()并将数字转换为整数