是否可以在Python中不使用表达式进行while循环? 我知道您可以使用其他语言执行以下操作:
while(flag) {};
我正在尝试在Python中做类似的事情,但是找不到答案。 这是我到目前为止的内容:
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()
我不做任何表情的原因是,如果第一只乌龟越界,我希望第二只乌龟不动。另外,我也不想在函数中使用return语句。
答案 0 :(得分:8)
您正在寻找pass关键字。
while (flag):
pass
答案 1 :(得分:2)
下面是exclude
对代码的重做,每个人都建议使用implementation ('your conflict library') {
exclude "com.android.support" }
以及其他一些样式和习惯用法更改以加强代码:
var asyncTask = DoSomethingAsync();
asyncTask.Start();
log.Info("Started!");
while (asyncTask.Status == TaskStatus.Running)
{
log.Info("Still running ...");
await Task.Delay(1000);
}
log.Info("Complete!);