[英]How do I break out of a python while loop in this case?
I don't know how to break out of a python while loop when turtle arrived on t.ycor == 0
当海龟到达t.ycor == 0
时,我不知道如何跳出 python while 循环
import turtle
import msvcrt
turtle.setup(700, 300, 0, 0)
t=turtle.Turtle()
t.up()
t.goto(-300,0)
t.down()
t.write("Start\n(" + str(t.xcor()) + ", " + str(t.ycor()) + ")", False, "center", ("",15))
while True:
if msvcrt.kbhit():
c = msvcrt.getch().decode(encoding = 'UTF-8')
if c == 'j':
t.left(3)
elif c == 'k':
t.right(3)
elif c == 'g':
t.forward(5)
t.right(3)
while t.ycor() > 0:
t.forward(10)
t.right(3)
t.write("End\n(" + str(t.xcor()) + ", " + str(t.ycor()) + ")", False, "center", ("",15))
print("When angle is", str(t.heading()), ", difference of x position is ", str(t.xcor()+300))
input("Press ENTER key to end")
I want to break out of while True:
after while t.ycor() > 0:
is done, but I don't know how....我想突破while True:
after while t.ycor() > 0:
已完成,但我不知道如何....
Sounds like you are searching for this:听起来你正在寻找这个:
while True:
if msvcrt.kbhit():
c = msvcrt.getch().decode(encoding = 'UTF-8')
if c == 'j':
t.left(3)
elif c == 'k':
t.right(3)
elif c == 'g':
t.forward(5)
t.right(3)
while t.ycor() > 0:
t.forward(10)
t.right(3)
break
If so, a better solution would be to eleminate the "while True:", because of the break the while loop is only used once - so the while is unnecessary.如果是这样,更好的解决方案是删除“while True:”,因为中断 while 循环只使用一次 - 所以 while 是不必要的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.