2014-04-06 3 views
0

도움을 주셔서 미리 감사드립니다. if else 문에 문제가 있습니다. 아래는 제 코드입니다. 기본적으로 else:이 입력되면 데이터가없고 나머지 코드가 실행되지 않아야 함을 의미합니다. 내가해야 할 일은 else:이 입력되었을 때입니다. 새로운 randx와 randy 값을 반환해야합니다. 그런 다음 if (dem_arr[randx, randy] > -100):에 다시 입력해야합니다. 나는 성공하지 못하고 사용하면서 노력했다.Python : 조건부 결과를 반환하고 조건문을 다시 입력하는 방법

neighbors = [(-1,-1), (-1,0), (-1,1), (0,1), (1,1), (1,0), (1,-1), (0,-1)] 
mask = np.zeros_like(dem_arr, dtype = bool) 
stack = [(randx, randy)] # push start coordinate on stack 
counterStack = [(randx, randy)] 

if (dem_arr[randx, randy] > -100): 
    count = 0 
    while count <= 121: 
     x, y = stack.pop() 
     mask[x, y] = True 
     for dx, dy in neighbors: 
      nx, ny = x + dx, y + dy 
      if (0 <= nx < dem_arr.shape[0] and 0 <= ny < dem_arr.shape[1] and dem_arr[x, y] > -100 and dem_arr[nx, ny] > -100 and not mask[nx, ny] and abs(dem_arr[nx, ny] - dem_arr[x, y]) <= 5): #set elevation differnce 
       stack.append((nx, ny)) #if point is selected (true) array position gets added to stack and process runs over again 
       if ((nx, ny) not in counterStack): 
        counterStack.append((nx, ny)) 
        dem_copy[(nx, ny)] = 8888 
        dem_copy[randx, randy] = 8888 
        count += 1 
else: #if enters else then need new randx and new randy points need to be returned and re-enter the above if(dem_arr...) 
    print 'Point chosen has no data' 
    randx = random.randint(0, row-1) 
    randy = random.randint(0, col-1) 

감사합니다.

-r

+1

당신은'while','break'와 'CONTINUE'를 사용하여 작업을 수행 할 수 있습니다. –

+0

while, break, continue 문을 제대로 구조화 할 수 없었습니다. 예제를 제공하거나 내 코드에서 어디에서 이러한 진술을 입력해야하는지 말해 줄 수 있습니까? 조언 해주셔서 감사합니다! – rharmony

답변

0

이와 같이 코드를 수정하십시오.

전에 :

if (dem_arr[randx, randy] > -100): 
    ... 
else: 
    ... 

후 :

while(True): 
    if (dem_arr[randx, randy] > -100): 
     ... 
     break#break from this while-loop. 
    else: 
     ... 
     continue#go back and continue this while-loop. 
관련 문제