有没有办法根据用户输入用 Turtle 绘制多个圆圈?
Is there a way to draw multiple circles with Turtle based on user input?
我想创建如下所示的内容:many circles of the same size next to each other
但是,我希望圈数由用户输入确定。我似乎无法找到任何有关如何处理此问题的信息。
这是我目前所拥有的,但它没有实现我的目标。
import turtle
print("How many circles?")
circnum = input()
#Summoning the turtle
t = turtle.Turtle()
#circling the circle
for i in circnum:
r = 25
t.circle(r)
非常感谢!
你需要制作 circnum
一个数字,这样你就可以制作一个 range
来迭代,你需要在圆圈之间移动乌龟,这样你就不仅仅是画一样的了一遍又一遍地在自己上面画圈。
import turtle
print("How many circles?")
circnum = int(input())
#Summoning the turtle
t = turtle.Turtle()
#circling the circle
for _ in range(circnum):
t.circle(25)
t.forward(5)
我同意@Samwise 的建议 (+1),但如果您使用的是标准 Python 3 turtle,而不是某些旧版本或子集,我建议您摆脱 input()
然后继续满龟:
from turtle import Screen, Turtle
RADIUS = 25
DISTANCE = 10
screen = Screen()
number_circles = screen.numinput("A Circle in a Spiral", "How many circles?", default=10, minval=1, maxval=30)
if number_circles:
# Summoning the turtle
turtle = Turtle()
turtle.speed('fast') # because I have little patience
# Circling the circle
for _ in range(int(number_circles)): # numinput() returns a float
turtle.circle(RADIUS)
turtle.forward(DISTANCE)
screen.exitonclick()
else:
# user hit 'Cancel' in the number input dialog
screen.bye()
我想创建如下所示的内容:many circles of the same size next to each other
但是,我希望圈数由用户输入确定。我似乎无法找到任何有关如何处理此问题的信息。
这是我目前所拥有的,但它没有实现我的目标。
import turtle
print("How many circles?")
circnum = input()
#Summoning the turtle
t = turtle.Turtle()
#circling the circle
for i in circnum:
r = 25
t.circle(r)
非常感谢!
你需要制作 circnum
一个数字,这样你就可以制作一个 range
来迭代,你需要在圆圈之间移动乌龟,这样你就不仅仅是画一样的了一遍又一遍地在自己上面画圈。
import turtle
print("How many circles?")
circnum = int(input())
#Summoning the turtle
t = turtle.Turtle()
#circling the circle
for _ in range(circnum):
t.circle(25)
t.forward(5)
我同意@Samwise 的建议 (+1),但如果您使用的是标准 Python 3 turtle,而不是某些旧版本或子集,我建议您摆脱 input()
然后继续满龟:
from turtle import Screen, Turtle
RADIUS = 25
DISTANCE = 10
screen = Screen()
number_circles = screen.numinput("A Circle in a Spiral", "How many circles?", default=10, minval=1, maxval=30)
if number_circles:
# Summoning the turtle
turtle = Turtle()
turtle.speed('fast') # because I have little patience
# Circling the circle
for _ in range(int(number_circles)): # numinput() returns a float
turtle.circle(RADIUS)
turtle.forward(DISTANCE)
screen.exitonclick()
else:
# user hit 'Cancel' in the number input dialog
screen.bye()