如何在python中创建一个海龟循环?



如何在Python中创建海龟控制结构?在这个程序中,如果我输入N,它可以工作,但如果我输入Y,它会导致错误?有人能重写这段代码并提供反馈吗?谢谢。

turtle.shape("turtle")
s = turtle.getscreen()
t = turtle.Turtle()
def triangle():
for i in range(3):
turtle.forward(50)
turtle.right(360/3)

def square():
for i in range(4):
turtle.forward(50)
turtle.right(360/4)
def circle():
for i in range(5):
turtle.circle(90)

answer = input(" Please type any shape you desire. Options: Triangle, Square, and Circle(MUST BE LOWERCASE)")
if answer ==('triangle'):
triangle()
elif answer == ('square'):
square()
elif answer == ('circle'):
circle()
else:
print ('Input is wrong')



下面是您的程序的大致工作版本。它不会显式地询问您是否要继续,而只是提示一个新的形状,如果您给它任何其他东西,它就会退出。你应该能够重新设计它,以适应你想要的"Okay, have a good day"接口:

from turtle import Screen, Turtle
def triangle():
for _ in range(3):
turtle.forward(50)
turtle.right(360/3)
def square():
for _ in range(4):
turtle.forward(50)
turtle.right(360/4)
def circle():
turtle.circle(-50/2)  # radius of 25, drawn clockwise
screen = Screen()
turtle = Turtle()
turtle.shape('turtle')
while True:
answer = screen.textinput("Shape you desire", "triangle, square, or circle (Must be lowercase)?")
turtle.clear()
if answer == 'triangle':
triangle()
elif answer == 'square':
square()
elif answer == 'circle':
circle()
else:
break
screen.bye()

最新更新