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()
该如何将两个for循环和在一起呢
- 写回答
- 好问题 0 提建议
- 关注问题
- 邀请回答
-
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()
但是这个并不是楼主想要的结果,我猜测楼主想得到这样的结果:

思路是用两个数组分别存半径和颜色,附上代码:
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()本回答被题主选为最佳回答 , 对您是否有帮助呢?评论 打赏 举报 编辑记录解决 1无用