Joker129 2022-04-02 21:03 采纳率: 100%
浏览 36
已结题

该如何将两个for循环和在一起呢

import turtle as t
def DrCircle(n,f):
    t.penup()
    t.goto(0,-n)
    t.pendown()
    t.pencolor('red')
    t.begin_fill()
    t.circle(n)
    t.fillcolor(f)
    t.end_fill()
for i in range(160,30,-60):
for z in ['yellow','blue','green']:
    DrCircle(i,z)
t.hideturtle()
t.done()

  • 写回答

1条回答 默认 最新

  • A-Chin 2022-04-03 09:41
    关注

    python是通过缩进来判断语句的归属。将语句for z in ['yellow','blue','green']:和语句DrCircle(i,z)各增加一个缩进。代码和运行结果如下:

    import turtle as t
    def DrCircle(n,f):
        t.penup()
        t.goto(0,-n)
        t.pendown()
        t.pencolor('red')
        t.begin_fill()
        t.circle(n)
        t.fillcolor(f)
        t.end_fill()
    for i in range(160,30,-60):
        for z in ['yellow','blue','green']:
            DrCircle(i,z)
    t.hideturtle()
    t.done()
    

    img

    但是这个并不是楼主想要的结果,我猜测楼主想得到这样的结果:

    img

    思路是用两个数组分别存半径和颜色,附上代码:

    import turtle as t
    def DrCircle(n,f):
        t.penup()
        t.goto(0,-n)
        t.pendown()
        t.pencolor('red')
        t.begin_fill()
        t.circle(n)
        t.fillcolor(f)
        t.end_fill()
        
    r = range(160,30,-60)
    c = ['yellow','blue','green']
    for i in range(len(r)):
        DrCircle(r[i], c[i])
    t.hideturtle()
    t.done()
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录

报告相同问题?

问题事件

  • 系统已结题 4月11日
  • 已采纳回答 4月3日
  • 创建了问题 4月2日