Hello everyone just I want to ask if I can make this code with only one loop, is it possible ??
import turtle
turtle.bgcolor("white")
turtle.pensize(2)
turtle.speed(0)
m =turtle.Turtle()
m.hideturtle()
m.penup()
m.goto(-350,50)
m.showturtle()
m.pendown()
m.goto(5,5)
for i in range(6):
for colors in ["brown","red","magenta","blue","green","orange"]:
turtle.color(colors)
turtle.circle(100)
turtle.right(10)
CodePudding user response:
You could do something like this by multiplying the list holding your colors, since you don't use the i value anywhere the program will work the same:
import turtle
turtle.bgcolor("white")
turtle.pensize(2)
turtle.speed(0)
m =turtle.Turtle()
m.hideturtle()
m.penup()
m.goto(-350,50)
m.showturtle()
m.pendown()
m.goto(5,5)
for colors in ["brown","red","magenta","blue","green","orange"] * 36:
turtle.color(colors)
turtle.circle(100)
turtle.right(10)
However, I don't think it's worth it because it will make your code less readable.
CodePudding user response:
You could repeat the code in your first loop for each colour, like this:
import turtle
turtle.bgcolor("white")
turtle.pensize(2)
turtle.speed(0)
m =turtle.Turtle()
m.hideturtle()
m.penup()
m.goto(-350,50)
m.showturtle()
m.pendown()
m.goto(5,5)
for i in range(36):
turtle.color("brown")
turtle.circle(100)
turtle.right(10)
turtle.color("red")
turtle.circle(100)
turtle.right(10)
turtle.color("magenta")
turtle.circle(100)
turtle.right(10)
turtle.color("blue")
turtle.circle(100)
turtle.right(10)
turtle.color("green")
turtle.circle(100)
turtle.right(10)
turtle.color("orange")
turtle.circle(100)
turtle.right(10)
But I don't recommend doing that because it is inefficient, messy, and bad practice