2013-04-10 3 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

1 문자 변수 이름을 사용하지 마십시오. 처음에는 코드에서 쉽게 식별 할 수 없습니다. 의미있는 이름을 사용하면 코드를 훨씬 쉽게 읽을 수 있습니다. 물론, 내가 생각할 수있는 유일하게 의미있는 1 문자 변수 이름은 x, y 및 z – volcano

답변

0

이름은 문자열입니다. 당신은 당신이 값을 먼저 중단해야 할

for i, j in name:

사용하고 튜플을 만들 수 없습니다.

이런

아마 어떤 :

N = (name.split가 ('') [0] name.split는 ('') [1]) - 좌표를 가정하면 의해 분리된다 ','

+0

이므로 코드 행은 어디에 있습니까? 'i, j의 경우'대신 ' – blablabla

+0

'코드 줄이 데모 일뿐입니다. 'name'을'n'으로 대체해야하지만'n'보다는 더 나은 이름을 사용해야합니다. 그것은 for 루프 앞에 물론 온다. – WeaselFox

+0

ok와 그 목적은 실제 값으로 입력되는 것을 나눌 것인가? 이 방법을 사용하면 사용자가 메시지가 나타나면 좌표를 입력해야합니다. 그들은 'x, y'처럼 그것을 입력할까요? – blablabla

0

"raw_input"은 문자열을 반환합니다. 변수를 split()하고 숫자를 정수로 변환해야합니다.

관련 문제