简体   繁体   English

从Anaconda执行时如何在Python中结束乌龟循环?

[英]How to end the turtle loop in Python while executing from Anaconda?

I have the below code: The loop does not end and the turtle keeps making circle. 我有以下代码:循环没有结束,乌龟不断绕圈。

    import turtle
    from turtle import Turtle
    from random import randint
    window=turtle.Screen()
    t = turtle.Turtle()
    #circle
    t.reset()
    while True:
        t.forward(2)
        t.right(1)
    if abs(pos()) < 1:
        break

I am executing this from Jupyter notebook. 我正在从Jupyter笔记本执行此操作。 The same code works well if i execute from command prompt. 如果我从命令提示符处执行,则相同的代码效果很好。 Please advise!! 请指教!!

Thanks 谢谢

The code doesn't work at the command prompt as written, despite you saying it does (twice.) The problem is the pos() call -- it throws NameError: name 'pos' is not defined . 尽管您说过(两次),但代码在命令提示符下不起作用(问题是两次)。问题是pos()调用-引发NameError: name 'pos' is not defined If you had also thrown in an from turtle import * among your multiple turtle imports, then it would run, but incorrectly as you would be moving your turtle t , but testing the default turtle's position. 如果你在一个甩也from turtle import *您多次龟进口中,那么它会运行,但不正确的,你会被感动你的t ,但在测试默认龟的位置。 In this simplified form it should work: 以这种简化形式,它应该可以工作:

import turtle

t = turtle.Turtle()

while True:
    t.forward(2)
    t.right(1)
    if abs(t.pos()) < 1:
        break

turtle.done()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM