Ryderius
Ryderius

Reputation: 55

turtle-graphics: Using if-else statement to determine turtle parameters

I have an if-else statement that I am using to determine the turtle's pen color and line width. When I run my program, it will plot the appropriate points, but only draw them in 'white' with the minimum line width. Within the 'windspeeds' list, there are numerous values that exceed the minimum standard of 74 in my if-else statement. What causes the if-statement to not pass the value into the correct statement?

t.penup()
t.setpos(longitudes[0], latitudes[0])
t.pendown()
i = 0
while i < len(latitudes):
    t.setpos(longitudes[i], latitudes[i])
    for index in windspeeds:
        if index < 74:
            t.pencolor("white")
            t.width(2)
        elif 74 <= index <= 95:
            t.pencolor("blue")
            t.width(4)
        elif 96 <= index <= 110:
            t.pencolor("green")
            t.width(6)
        elif 111 <= index <= 129:
            t.pencolor("yellow")
            t.width(8)
        elif 130 <= index <= 156:
            t.pencolor("#FFA500")
            t.width(10)
        elif 157 <= index:
            t.pencolor("red")
            t.width(12)
    i += 1

Upvotes: 1

Views: 1559

Answers (1)

onlynone
onlynone

Reputation: 8289

I think the problem is that you're iterating through all of windspeeds for each point, but you're not moving the pen. The last element in windspeeds is likely less than 74.

So for each position you're going through all the windspeeds, changing the color and width multiple times without moving the pen. So after moving the pen, you always end up back with t.pencolor("white") and t.width(2).

Is the windspeeds array supposed to be indexed like the longitudes and latitudes arrays? If so you probably want something like:

t.penup()
t.setpos(longitudes[0], latitudes[0])
t.pendown()
for i in range(len(latitudes)):
    t.setpos(longitudes[i], latitudes[i])
    speed = windspeeds[i]
    if speed < 74:
        t.pencolor("white")
        t.width(2)
    elif speed < 94:
        t.pencolor("blue")
        t.width(4)
...
    elif speed < 157:
        t.pencolor("#FFA500")
        t.width(10)
    else:
        t.pencolor("red")
        t.width(12)

Upvotes: 1

Related Questions