2017-10-21 3 views
0

나는 randrange가 포인트를 줄 때마다 25 픽셀을 그려야하는 작은 프로그램을 작성 중이다. 또한 폭탄이나 광산으로 작동하는 4 개의 빨간색 상자가 있습니다. 선의 x, y가 getColor 함수에 의해 빨간색이면 var 'color'는 == 빨간색이됩니다. 따라서 while 루프를 중지하면 줄이 계속 켜지지 않습니다. 이것은 내가 경기장에 그려 넣은 파란색 점들에 대해서도 동일한 원하는 기능입니다. 나는 내 프로그램이 불행히도이 방법으로 작동하지 않는 것으로 나타났습니다. 나는 이것을 고칠 수있는 방법에 대한 제안?while 루프가 올바르게 멈추지 않을 때

from random import * 
def main(): 
    #draw 
    pic = makeEmptyPicture(600, 600, white) 
    show(pic) 

    #for the 4 boxes 
    boxCount = 0 
    #while statement to draw 
    while boxCount < 4: 
     addRectFilled(pic, randrange(0,576), randrange(0,576), 25, 25, red) 
     addArcFilled(pic, randrange(0,576), randrange(0,576), 10, 10, 0, 360, blue) 
     boxCount = boxCount + 1 
    repaint(pic) 

    #vars for while statement 
    newX = 0 
    newY = 0 
    oldX = 0 
    oldY = 0 
    robotcount = 0 
    finished = 0 
    safe = 0 
    triggered = 0 
    #while loop, stops @ step 750, or when a px == red/blue 
    while robotcount < 750 or color == red or color == blue: 

     oldX = newX 
     oldY = newY 
     #how to generate a new line poing +25/-25 
     newX = newX + randrange(-25, 26) 
     newY = newY + randrange(-25, 26) 
     #if statements to ensure no x or y goes over 599 or under 0 
     if newX > 599 or newX < 0: 
      newX = 0 
     if newY > 599 or newY < 0: 
      newY = 0 
     #functions to get pixel color of x,y 
     px = getPixel(pic, newX, newY) 
     color = getColor(px) 
     #draw the line from old to new, and also add +1 count for robot's steps 
     addLine(pic, oldX, oldY, newX, newY, black) 
     robotcount = robotcount + 1 

    #if statement to determine why the while loop stops 
    if color == red: 
     triggered = 1 
     printNow("trig") 
    if color == blue: 
     safe = 1 
     printNow("safe") 
    if robotcount == 750: 
     finished = 1 
     printNow("Fin") 
+0

어떻게'red'와'blue'가 정의되어 있습니다 4,루프와 수정 귀하의 조건합니다 (!= 주)? – Iguananaut

+1

직접이 문제를 디버그하려고 했습니까? 약간의 printf 디버깅을하면 시간이 걸릴 것입니다. https://ericlippert.com/2014/03/05/how-to-debug-small-programs/ – jdv

+0

@Iguananaut는'color'가 정의 된 곳과 더 비슷합니다 ... –

답변

0

당신이 달성하고자하는 :

#while loop, stops @ step 750, or when a px == red/blue 

이 작동하지 않습니다

for robotcount in range(750): 
    if color == red or color == blue: 
     break 
:

while robotcount < 750 or color == red or color == blue: 

그것은 간단하게 될 것 대신 for 루프를 사용하는

를 사용할 수도 있습니다.

while robotcount < 750 or color != red or color != blue: 
+0

에 미리 정의되어 있습니다. 빨간색 상자를 통해 선을 그릴 때 마지막 if then 문마다 다른 문자열을 중지하고 출력하지 않습니다. – ohGosh

+0

for 문만 사용하고 while 문은 사용하지 않아도됩니다. – ohGosh

+0

'while'을 사용하여 대안을 추가하십시오. –

관련 문제