problem with existing condition in while python. Turtle

Asked

Viewed 224 times

0

I’m having a hard time solving an exercise in a programming book.

The exercise asks me to create two turtles and move them randomly, so that each turtle walks 50 units forward, then turn either left, or right, 90 degrees. Turtles must be "born" in a random place within the bounded space of the screen. They must walk freely until they 'touch'. When this occurs, the program must stop. I was able to write everything down, until this final condition.

My difficulty is there at the end of the code, in the function caminho_ate_parada. my idea is that as long as the coordinates (x, y) of alex and Tess are different, the program continues running. If the coordinates (x, y) of alex are equal to the coordinates (x, y) of Tess, the program must stop. the strange thing is that sometimes the program stops when the two turtles are at the same value of the axis (x), however at different values of the axis (y).

What can I add to the code to solve this?

As I can not send more than 2 files here, I created a blog just to put the code images

link from my blog with the images

import random
import turtle

alex = turtle.Turtle()
tess = turtle.Turtle()
wn = turtle.Screen()

tess.shape('turtle')
tess.color('blue')
tess.speed(0)
alex.shape('arrow')
alex.color('red')
alex.speed(0)

def isInScreenAlex(w,t1):                                
    leftBound = - w.window_width()//2 + 100              
    rightBound = w.window_width()//2 - 100               
    topBound = w.window_height()//2 - 100                
    bottomBound = -w.window_height()//2 + 100            

    turtleX = t1.xcor()                                  
    turtleY = t1.ycor()                                  
    stillInAlex = True                                   
    if turtleX > rightBound or turtleX < leftBound:      
        stillInAlex = False                              
    if turtleY > topBound or turtleY < bottomBound:      
        stillInAlex = False                              
    return stillInAlex                                   


def isInScreenTess(w,t2):                                
    leftBound = - w.window_width()//2 + 100              
    rightBound = w.window_width()//2 - 100               
    topBound = w.window_height()//2 - 100                
    bottomBound = -w.window_height()//2 + 100            

    turtleX = t2.xcor()                                  
    turtleY = t2.ycor()                                   
    stillInTess = True                                   
    if turtleX > rightBound or turtleX < leftBound:      
        stillInTess = False                              
    if turtleY > topBound or turtleY < bottomBound:      
        stillInTess = False                              
    return stillInTess                                   


def compXa():                                                                              
    MaxXAxisA = int(random.randrange(- wn.window_width()//5, wn.window_width()//5 ))        
    while MaxXAxisA % 50 != 0:                                                              
        MaxXAxisA = MaxXAxisA + 1                                                           
    return MaxXAxisA                                                                        

def compYa():                                                                              
    MaxYAxisA = int(random.randrange(-wn.window_height()//5, wn.window_height()//5 ))       
    while MaxYAxisA % 50 != 0:                                                              
        MaxYAxisA = MaxYAxisA + 1                                                          
    return MaxYAxisA                                                                       

xa = int(compXa())
ya = int(compYa())

alex.penup()                                                                               
alex.goto(xa, ya)                                                                          
alex.pendown()                                                                            


def compXt():
    MaxXAxisT = int(random.randrange(- wn.window_width()//5, wn.window_width()//5))
    while MaxXAxisT % 50 != 0:
        MaxXAxisT = MaxXAxisT + 1
    return MaxXAxisT

def compYt():                                                                             
    MaxYAxisT = int(random.randrange(-wn.window_height()//5, wn.window_height()//5))
    while MaxYAxisT % 50 != 0:
        MaxYAxisT = MaxYAxisT + 1
    return MaxYAxisT

xt = int(compXt())
yt = int(compYt())

tess.penup()                                                                              
tess.goto(xt, yt)                                                           
tess.pendown()                                                                            


def positionAlex():
    if isInScreenAlex(wn, alex) == True:

        coin = random.randrange(0,2)
        if coin == 0:
            alex.left(90)
        else:                                                                            
            alex.right(90)                                                             

        alex.forward(50)

    else:
        alex.left(180)
        alex.forward(50)


def positionTess():
    if isInScreenTess(wn, tess) == True:

        coin2 = random.randrange(0,2)
        if coin2 == 0:
            tess.left(90)
        else:                                                                             
            tess.right(90)                                                                

        tess.forward(50)

    else:
        tess.left(180)
        tess.forward(50)



def caminho_ate_parada():    
    while tess.xcor() != alex.xcor() and tess.ycor() != alex.ycor():                      
        tess.xcor()                                                                       
        tess.ycor()
        alex.xcor()                         
        alex.ycor()                                                                       
        positionAlex()
        positionTess()


caminho_ate_parada()
wn.exitonclick()
  • Should be or, nay and. You want the program to continue running as long as the x coordinates are different or the y coordinates are different, not as long as the x coordinates and y coordinates are different.

  • but the coordinates x of Tess and alex should be equal and the coordinates y also. I want the 2 expressions to be false so that the program stops. since it is a pair of coordinates and the values of x must be equal to each other and the values of y should be equal to each other. or and it didn’t work. I think the problem is in defining the value of each coordinate for each separate interaction. since each movement generates a new co-ordination and this new co-ordination needs to be evaluated

1 answer

0

The program is stopping because of the condition of its while requires two expressions to be true for the program to continue. That is, the while rotate while x1 is different from x2 and (and), at the same time, y1 is different from y2. If only one of these expressions returns false, the while will end. In other words, not necessarily the two expressions need to return false so that the while finish.

The simplest solution that comes to mind is to create a while infinity and put a if comparing equality instead of difference:

while True:
    if x1 == x2 and y1 == y2:
        break
    # chamadas das suas funções

That way only when everything is equal while will end.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.