0

So i have a challenge for university and it involves make a dynamic pie chart with a single seperated segment

i already have

    chart_title = 'Tourism GDP by States/Territories in Australia'
    segment_labels = ['QLD', 'VIC', 'NSW', 'SA', 'WA', 'TAS', 'NT', 'ACT']
    percentages = [0.24, 0.22, 0.328, 0.06, 0.082, 0.03, 0.02, 0.02]

    from turtle import *

    radius = 200


    penup()
    forward(radius)
    left(90)
    pendown()
    color('palegreen')
    begin_fill()
    circle(radius)
    end_fill()
    home()
    right(90)
    color('black')

    def segment(percentages):
        rollingPercent = 0
        radius=200
        for percent in percentages:
            segment = percent * 360
            rollingPercent += segment
            setheading(rollingPercent)
            pendown()
            forward(radius)
            penup()
            home()

is this right? because when i execute the code it just draws a green circle and does not draw any segments in the pie chart

user3500349
  • 1
  • 1
  • 1

2 Answers2

1

You code consists of a few parts.

Section 1:

chart_title = 'Tourism GDP by States/Territories in Australia'
segment_labels = ['QLD', 'VIC', 'NSW', 'SA', 'WA', 'TAS', 'NT', 'ACT']
percentages = [0.24, 0.22, 0.328, 0.06, 0.082, 0.03, 0.02, 0.02]
radius = 200

Section 2:

from turtle import *

Section 3:

penup()
forward(radius)
left(90)
pendown()
color('palegreen')
begin_fill()
circle(radius)
end_fill()
home()
right(90)
color('black')

section 4:

def segment(percentages):
    rollingPercent = 0
    radius=200
    for percent in percentages:
        segment = percent * 360
        rollingPercent += segment
        setheading(rollingPercent)
        pendown()
        forward(radius)
        penup()
        home()

In section one, you define some variables In section two you import the turtle module (library) In section 3 you execute some functions from the turtle library which draw a green circle

Now the important bit. In section 4, you define a function (called segment) which can draw segments. However, it will not draw segments until you explicitly ask it to. If you aren't familiar with what a function is, you should read some tutorials on the matter. They are quite important to understand (see here, here and here).

So while your function to draw segments has been defined, you are not calling the function (running the code in the function). Your function takes one argument (parameter) percentages which is a list of percentages for the segments. Note the variable name percentages in this case refers to a local variable that only exists within the function segment, it does not necessarily refer to the list you define in section 1 of your code (but it can). To understand what I mean about local variables, read this.

So you need to call your function. To do this, you'll need to add the line of code segment(percentages), which calls the function segment and passes in the list of percentages as an argument.

Full code:

chart_title = 'Tourism GDP by States/Territories in Australia'
segment_labels = ['QLD', 'VIC', 'NSW', 'SA', 'WA', 'TAS', 'NT', 'ACT']
percentages = [0.24, 0.22, 0.328, 0.06, 0.082, 0.03, 0.02, 0.02]

from turtle import *

radius = 200


penup()
forward(radius)
left(90)
pendown()
color('palegreen')
begin_fill()
circle(radius)
end_fill()
home()
right(90)
color('black')
def segment(percentages):
    rollingPercent = 0
    radius=200
    for percent in percentages:
        segment = percent * 360
        rollingPercent += segment
        setheading(rollingPercent)
        pendown()
        forward(radius)
        penup()
        home()
segment(percentages)

It seems pretty clear from your comments that you need to familiarise yourself with functions in Python, so I suggest reading some tutorials and getting the basics down. It will make life easier in the future and really open up the capabilities of programming to you.

Community
  • 1
  • 1
three_pineapples
  • 11,579
  • 5
  • 38
  • 75
  • i am really new to python this is my 1st year in an IT degree, i will have a read through of the links you meantioned and also try to get myself more familiarised with the functions in python. also is there anyway to separate the largest segment from the rest of the pie chart – user3500349 Apr 05 '14 at 12:57
  • @user3500349 Unfortunately, A) I Don't know much about turtle and B) As this appears to be an assignment of some kind, I can't write large sections of code for you. The best idea would be to have an attempt at separating out a segment yourself, and if it doesn't quite do what you want, ask a new question on stack-overflow specifically about that problem. – three_pineapples Apr 05 '14 at 23:59
0

You've defined the function segment to do certain things, but you've never actually called or run it - that's why it's not doing anything other than drawing a circle for you. you can do so by segment() on a new, non-indented, line.