【问题标题】:How to move the turtle where I want to go如何将乌龟移动到我想去的地方
【发布时间】:2016-09-02 02:23:03
【问题描述】:

所以我试图在我的编程课程中完成这个问题,它涉及到用乌龟画东西。 基本上,我正在尝试绘制城市天际线,因此程序需要在一行中读取用户的多个输入(建筑物的高度)。我可以用它来绘制建筑物,但它只使用最后一个 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

请帮忙!

这是一张图片: Description of the question Some of the specifics

【问题讨论】:

  • 为什么你有两次for i in y:?有没有必要循环y两次?
  • 你为什么要连续两次迭代for i in y

标签: python turtle-graphics


【解决方案1】:

取出第二个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 获取您的绘图代码之前前进到最终值。

【讨论】:

    猜你喜欢
    • 2014-05-22
    • 1970-01-01
    • 2021-10-01
    • 2022-10-15
    • 2020-03-03
    • 2021-07-26
    • 1970-01-01
    • 2010-11-03
    • 1970-01-01
    相关资源
    最近更新 更多