2

Is is possible to have a while loop in Python with no expressions? I know in other languages you can do something like:

while(flag) {};

I'm trying to do something similar in Python but cannot find an answer. Here is what I have so far:

import turtle
from random import randrange

def is_in_screen(t, w): #CHECKS TO SEE IF STILL IN SCREEN
    flag = True
    r = w.window_width() / 2
    l = r * -1
    u = w.window_height() / 2
    d = u * -1

    x_cor = t.xcor()
    y_cor = t.ycor()

    if (x_cor < l or x_cor > r or y_cor < d or y_cor > u):
        flag = False
    return flag

def move_to(t, w): #MOVE IN RANDOM DIRECTION AND RANDOM DISTANCE
    t.forward(randrange(1, 100))
    if (randrange(1, 2) == 1):
        t.left(randrange(1, 180))
    else:
        t.right(randrange(1, 180))
    return is_in_screen(t, w)

def random_movement(t1, t2, w):
    while (move_to(t1, w) and move_to(t2, w)): #<<<<<<<<LOOP IN QUESTION
        i = 0 

def main():
    t1 = turtle.Turtle()
    t2 = turtle.Turtle()
    w = turtle.Screen()

    t1.color("green")
    t2.color("purple")

    random_movement(t1, t2, w)

    w.exitonclick()

main()

The reason I'm trying to do no expressions is because I want the second turtle to not move if the first turtle goes out of bounds. Also, I do not want return statements in the function.

5
  • 5
    The pass statement exists for this exact need. Commented Oct 7, 2018 at 4:22
  • 2
    I think what you are looking for is the command "pass". You can put this after the while and it will do nothing in the loop. Commented Oct 7, 2018 at 4:23
  • 2
    you can go for "pass" in python.... if you want to use while without any condition means it will become always true can do it like "while True:" Commented Oct 7, 2018 at 4:25
  • 3
    Such a loop is going to block your whole program with a lot of CPU load until the condition is met. Commented Oct 7, 2018 at 4:29
  • 3
    While you can use pass and it'll allow the program to run, code like while(<test with no side effects>): pass will hopelessly deadlock your program. Buyer beware. Commented Oct 7, 2018 at 5:12

2 Answers 2

8

You're looking for the pass keyword.

while (flag):
    pass
Sign up to request clarification or add additional context in comments.

1 Comment

Parens are very optional
2

Below is a rework of your code with the while expr: pass that everyone's suggesting along with some other style and idiom changes to tighten up the code:

from turtle import Screen, Turtle
from random import randrange

def is_in_screen(turtle, screen):
    r = screen.window_width() / 2
    u = screen.window_height() / 2

    x, y = turtle.position()

    return -r < x < r and -u < y < u

def move_to(turtle, screen):
    turtle.forward(randrange(1, 100))

    turtle.left(randrange(-180, 180))  # negative left turn is a right turn

    return is_in_screen(turtle, screen)

def random_movement(turtle_1, turtle_2, screen):
    while move_to(turtle_1, screen) and move_to(turtle_2, screen): pass

screen = Screen()

t1 = Turtle()
t1.color("green")

t2 = Turtle()
t2.color("purple")

random_movement(t1, t2, screen)

screen.exitonclick()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.