簡體   English   中英

從Anaconda執行時如何在Python中結束烏龜循環?

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

我有以下代碼:循環沒有結束,烏龜不斷繞圈。

    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

我正在從Jupyter筆記本執行此操作。 如果我從命令提示符處執行,則相同的代碼效果很好。 請指教!!

謝謝

盡管您說過(兩次),但代碼在命令提示符下不起作用(問題是兩次)。問題是pos()調用-引發NameError: name 'pos' is not defined 如果你在一個甩也from turtle import *您多次龜進口中,那么它會運行,但不正確的,你會被感動你的t ,但在測試默認龜的位置。 以這種簡化形式,它應該可以工作:

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