![]() |
Turtle Bar Chart Colour fill question - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Turtle Bar Chart Colour fill question (/thread-1997.html) |
Turtle Bar Chart Colour fill question - JRod - Feb-09-2017 Hey, I am using Turtle to learn how if and elif statements work. I have a quick program that draws a bar chart... here it is... import turtle john = turtle.Turtle() xs = [48, 117, 200, 240, 160, 260, 220] #values for the heights of the bars def draw_bar(t, height): """ Get turtle t to draw one bar, of height. """ t.begin_fill() if v >=200: john.color("blue", "red") elif v >=100: john.color("blue", "yellow") else: john.color("blue", "green") t.left(90) t.forward(height)# Draw up the left side t.write(" "+ str(height)) t.right(90) t.forward(40) # Width of bar, along the top t.right(90) t.forward(height) # And down again! t.left(90) # Put the turtle facing the way we found it. t.end_fill() t.penup() t.forward(10) # Leave small gap after each bar t.pendown() wn = turtle.Screen() # Set up the window and its attributes wn.bgcolor("lightgreen") # Create john and set some attributes john.pensize(3) for v in xs: draw_bar(john, v)My question is, why does putting the condition for the bar fill colours work where it is now and not where I placed it the first time (I had it when I called the function)... like this... import turtle john = turtle.Turtle() xs = [48, 117, 200, 240, 160, 260, 220] #values for the heights of the bars def draw_bar(t, height): """ Get turtle t to draw one bar, of height. """ t.begin_fill() t.left(90) t.forward(height)# Draw up the left side t.write(" "+ str(height)) t.right(90) t.forward(40) # Width of bar, along the top t.right(90) t.forward(height) # And down again! t.left(90) # Put the turtle facing the way we found it. t.end_fill() t.penup() t.forward(10) # Leave small gap after each bar t.pendown() wn = turtle.Screen() # Set up the window and its attributes wn.bgcolor("lightgreen") # Create john and set some attributes john.pensize(3) for v in xs: draw_bar(john, v) if v >=200: john.color("blue", "red") elif v >=100: john.color("blue", "yellow") else: john.color("blue", "green")Does that make sense?? RE: Turtle Bar Chart Colour fill question - Larz60+ - Feb-09-2017 In the second script, you appear to be setting the color, but not drawing anything RE: Turtle Bar Chart Colour fill question - JRod - Feb-09-2017 When I run the second script it draws the bars but the fill colours are wrong! The first bar is filled in black!! RE: Turtle Bar Chart Colour fill question - ichabod801 - Feb-09-2017 You're setting the color after you draw the bar, you want to set it before you draw the bar. Whatever color the turtle has when it draws is what it draws. You don't set it after drawing to change what was just drawn. |