Feb-09-2017, 09:02 PM
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...
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??
