I am using turtle in python to create simple shapes that have a change of gradient starting at white and ending at black, black is 0,0,0. The way I have it set up currently is that it will continue to subtract beyond zero within my nested loop. I want my code to print the color black once it equals or is less than 0,0,0. I would appreciate it if someone could give me pointers and hints without straight up giving me the solution. Thanks!
a = int(input("Enter int angle:"))
q = int(input("Enter int length:"))
print("Click turtle screen to exit...")
r=1
g=1
b=1
import turtle
wn = turtle.Screen()
wn.bgcolor("white")
alex = turtle.Turtle()
alex.speed(500)
alex.pencolor("white")
for i in range (q):
alex.forward(i)
alex.left(a)
for c in range (q):
alex.pencolor(r,g,b)
r=(r-.0001)
g=(g-.0001)
b=(b-.0001)
wn.exitonclick()
In your final for loop, you should have
for c in range (q):
alex.pencolor(r,g,b)
if condition (color is black or less than black):
result (print black)
else:
r=(r-.0001)
g=(g-.0001)
b=(b-.0001)
If we make the color decrement 1 over the number of iterations, then the color should reach black on our last iteration:
from turtle import Screen, Turtle
screen = Screen()
angle = screen.numinput("Angle", "Enter angle: ", default=110, minval=1, maxval=359)
length = int(screen.numinput("Length", "Enter integer length: ", default=75, minval=10, maxval=200))
gray = 1.0
turtle = Turtle()
turtle.speed('fastest')
for _ in range(length):
turtle.pencolor(gray, gray, gray)
turtle.forward(length)
turtle.left(angle)
gray = gray - 1.0 / length
screen.exitonclick()
But, if that still gives you problems, you can wrap it in a max()
:
gray = max(0, gray - 1.0 / length)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.