下面是我绘制黄色房子的海龟计划。我无法找到一种方法来创建一个for
循环,以保持龟程序的简短。有没有人知道如何为下面的程序创建一个循环?
import turtle
import math
def yellowHouse () :
wn=turtle.Screen()
wn.title("Yellow House")
wn.bgcolor("Blue")
#sideLength = 200
house=turtle.Turtle()
Diagonal_length= 0.5*math.sqrt(2 *(200 ** 2 ))
house.pensize(5)
house.speed(4.6)
house.color("Yellow")
house.left(90)
house.forward(200)
house.right (135)
house.forward(Diagonal_length)
house.right(90)
house.forward(Diagonal_length)
house.left(135)
house.forward(200)
house.left(135)
house.forward(Diagonal_length)
house.right(90)
house.forward(Diagonal_length)
house.left(135)
house.forward(200)
house.right(135)
house.forward(Diagonal_length)
house.right(90)
house.forward(Diagonal_length)
house.right(45)
house.forward(200)
house.left(90)
wn.mainloop()
yellowHouse ()
答案 0 :(得分:0)
由于您正在重建一个形状的房子,我会选择标记而不是绘图:
from turtle import Turtle, Screen
def yellowHouse(side_length):
wn.register_shape("brick", ((0, 0), (-0.5, -0.5), (0.5, -0.5)))
house = Turtle('brick', visible=False)
house.shapesize(stretch_wid=side_length, outline=5)
house.color("yellow", wn.bgcolor())
house.penup()
for angle in range(360, 0, -90):
house.setheading(angle)
house.stamp()
house.forward(side_length)
house.stamp()
wn = Screen()
wn.title("Yellow House")
wn.bgcolor("blue")
yellowHouse(200)
wn.exitonclick()
此解决方案将房子打印在屏幕中央。你传递你想要的房子的大小。 (而且它的代码少了1/3。)
答案 1 :(得分:0)
创建循环是使程序代码简短的好方法,您可以通过多种方式实现这一点,在您的示例中:
方法1
使用循环绘制正方形,然后绘制对角线交叉线和顶部
import turtle
def yellowHouse() :
wn=turtle.Screen()
wn.title("Yellow House")
wn.bgcolor("Blue")
house=turtle.Turtle()
house.pensize(5)
house.speed(4.6)
house.color("Yellow")
side = 200
diag = (2*(side**2))**0.5
ang = 90
for i in range(4):
house.fd(side)
house.lt(ang)
house.lt(ang/2)
moves = [(diag,ang),(diag/2,ang),(diag/2,ang),(diag,ang)]
for (move,turn) in moves:
house.fd(move)
house.lt(turn)
wn.mainloop()
yellowHouse()
方法2
使用循环遍历步骤来绘制整个形状
import turtle
def yellowHouse() :
wn=turtle.Screen()
wn.title("Yellow House")
wn.bgcolor("Blue")
house=turtle.Turtle()
house.pensize(5)
house.speed(4.6)
house.color("Yellow")
side = 200
diag = (2*(side**2))**0.5
ang = 90
moves = [(side,ang),(side,ang),(side,ang),(side,ang),
(0,ang/2),(diag,ang),(diag/2,ang),(diag/2,ang),(diag,ang)]
for (move,turn) in moves:
house.fd(move)
house.lt(turn)
wn.mainloop()
yellowHouse()
注意我们不需要导入数学,因为我们只需要sqrt()
来计算我们可以使用**
指数运算符sqrt(x)
实现的平方根。 x**0.5
答案 2 :(得分:-1)
这是你的forloop代码,你必须只维护事件和事件值索引..
import turtle
import math
def yellowHouse() :
wn=turtle.Screen()
wn.title("Yellow House")
wn.bgcolor("Blue")
#sideLength = 200
house=turtle.Turtle()
Diagonal_length= 0.5*math.sqrt(2 *(200 ** 2 ))
event = ["pensize","speed","color","left","forward","right","forward","right","forward","left","forward","left","forward","right","forward","left","forward","right","forward","right","forward","right","forward","left"]
event_val = [5,4.6,"Yellow",90,200,135,Diagonal_length,90,Diagonal_length,135,200,135,Diagonal_length,90,Diagonal_length,135,200,135,Diagonal_length,90,Diagonal_length,45,200,90]
event_zip = zip(event, event_val)
for i,j in event_zip:
if type(j).__name__ == "str":
eval("house.{}('{}')".format(i,j))
else:
eval("house.{}({})".format(i,j))
yellowHouse()