Python Forum
Turtle Bar Chart Colour fill question
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Turtle Bar Chart Colour fill question
#1
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??
Reply


Messages In This Thread
Turtle Bar Chart Colour fill question - by JRod - Feb-09-2017, 09:02 PM
RE: Turtle Bar Chart Colour fill question - by JRod - Feb-09-2017, 09:13 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Turtle Star Fill Color Yellow-White Interchanging Color Effect codelab 9 3,333 Oct-25-2023, 09:09 AM
Last Post: codelab
Question convert autohotkey script to python / macro - press key when pixel get colour willson94d 1 5,088 Jan-01-2022, 08:13 PM
Last Post: Yoriz
  Noob question: why is shapesize() not working for my turtle adifrank 8 8,838 Sep-09-2020, 11:13 PM
Last Post: adifrank
  Adding colour to Python console Archangelos 4 3,772 Apr-15-2020, 05:15 PM
Last Post: newbieAuggie2019
  Is it possible to have my python shell change the backgrounds colour? OTO1012 2 4,825 Mar-07-2019, 09:32 PM
Last Post: woooee
  wn = turtle.screen() AttributeError: module 'turtle' has no attribute 'screen' Shadower 1 7,186 Feb-06-2019, 01:25 AM
Last Post: woooee
  Help! Turtle not working, even when we click the turtle demo in IDLE nothing happens. BertyBee 3 7,299 Jan-04-2019, 02:44 AM
Last Post: SheeppOSU
  Best way to set cell background colour in openpyxl Pedroski55 4 75,002 Aug-08-2018, 01:24 PM
Last Post: sumandas89
  I am trying to make a Colour Wars game, "Winning code" missing Ondrejoda 0 2,728 Feb-18-2018, 11:06 AM
Last Post: Ondrejoda
  Working with turtle graphics in python and colour slices of a pie graph... netrate 1 5,281 Feb-03-2018, 08:41 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020