如何将乌龟移动到我想去的地方
How to move the turtle where I want to go
所以我想在我的编程课程中完成这个问题,它涉及到用乌龟画东西。
基本上,我正在尝试绘制城市天际线,因此程序需要在一行(建筑物的高度)上读取用户的多个输入。我可以用它来绘制建筑物,但它只使用最后一个 y 值。
from turtle import *
h = input("Heights: ")
y = h.split()
nxc = -200
#Code for the background
fillcolor("darkslategray")
for i in y:
for i in y:
nyc = i
pencolor("black")
pendown()
begin_fill()
goto(nxc, nyc)
right(90)
forward(20)
right(90)
forward(nyc)
right(90)
forward(20)
right(90)
forward(nyc)
end_fill()
nxc = nxc + 20
请帮忙!
取出你的第二个 for
循环:
from turtle import *
h = input("Heights: ")
y = h.split()
nxc = -200
#Code for the background
fillcolor("darkslategray")
for i in y:
nyc = i
pencolor("black")
pendown()
begin_fill()
goto(nxc, nyc)
right(90)
forward(20)
right(90)
forward(nyc)
right(90)
forward(20)
right(90)
forward(nyc)
end_fill()
nxc = nxc + 20
第二个循环将始终到达终点,每次在退出之前更新 nyc
。因此,对于每次迭代,nyc
将在 Python 到达您的绘图代码之前前进到最终值。
所以我想在我的编程课程中完成这个问题,它涉及到用乌龟画东西。 基本上,我正在尝试绘制城市天际线,因此程序需要在一行(建筑物的高度)上读取用户的多个输入。我可以用它来绘制建筑物,但它只使用最后一个 y 值。
from turtle import *
h = input("Heights: ")
y = h.split()
nxc = -200
#Code for the background
fillcolor("darkslategray")
for i in y:
for i in y:
nyc = i
pencolor("black")
pendown()
begin_fill()
goto(nxc, nyc)
right(90)
forward(20)
right(90)
forward(nyc)
right(90)
forward(20)
right(90)
forward(nyc)
end_fill()
nxc = nxc + 20
请帮忙!
取出你的第二个 for
循环:
from turtle import *
h = input("Heights: ")
y = h.split()
nxc = -200
#Code for the background
fillcolor("darkslategray")
for i in y:
nyc = i
pencolor("black")
pendown()
begin_fill()
goto(nxc, nyc)
right(90)
forward(20)
right(90)
forward(nyc)
right(90)
forward(20)
right(90)
forward(nyc)
end_fill()
nxc = nxc + 20
第二个循环将始终到达终点,每次在退出之前更新 nyc
。因此,对于每次迭代,nyc
将在 Python 到达您的绘图代码之前前进到最终值。