2013-04-10 59 views
1

我需要創建代碼,它允許您檢查您輸入的座標是否位於特定區域內。到目前爲止,我有這樣的:如何檢查座標是否位於某個區域?

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個多值解包」指的是「名稱=的raw_input(」鍵入一個座標位置:「)」行。我究竟做錯了什麼?

+0

請不要使用單字母變量名稱。對於初學者來說,他們在代碼中不易辨別。有意義的名稱使代碼更易於閱讀。 當然,我可能會想到的唯一有意義的單字母變量名是x,y和z – volcano 2013-04-10 08:46:54

回答

0

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

for i, j in name:

你需要先破譯它的值,並創建一個元組。

也許是這樣的:

N =(name.split( ' ')[0],name.split(', ')[1]) - 假設座標由分離','

+0

,那麼代碼行會在哪裏?並將它取代'爲我,j的名字:' – blablabla 2013-04-10 10:11:58

+0

該行代碼只是一個示範。你應該用'n'替換'name',但使用比'n'更好的名字。它出現在for循環當然... – WeaselFox 2013-04-10 10:13:40

+0

好了,它的目的是將輸入內容分成實際值嗎?使用這種方式時,用戶必須在提示時輸入座標。他們會像'x,y'一樣輸入嗎? – blablabla 2013-04-10 10:16:10

0

「raw_input」返回一個字符串。你需要分割()它並將數字轉換爲整數

相關問題