Question

is there any example of creating a pie chart using only a turtle module and list given by a lecturer for phyton? Most result i found is usually add some other modules like pychart. really Need help on this for my test.

this are what given:- chart_title = 'Public Service Commission Expenditure in Queensland']

segment_labels = ['Safety', 'Education', 'Transport', 'Health', 'Other']

percentages = [0.1, 0.25, 0.16, 0.2, 0.29]

I managed to make the cirile up and even the lines. How do i incorporate in a way the line follow the values given???

this is my code..very amateurish , but this is my standard as i am relli struggling on this prgramming class.

for i in range(len(percentages)):

if i!=0:
    if i in range(len(percentages))>=0.25:
        goto(0,320)
        right(-205)
        pendown()
        goto(0,0)
        fd(320)
        color('dark green')
        width(4)
        penup()
    elif i in range(len(percentages))>=0.16:
        goto(0,320)
        right(-245)
        pendown()
        goto(0,0)
        fd(320)
        color('dark green')
        width(4)
        penup()

it seems to draw the exact same thing even if i set the elif to it. may someone explain to me why and how do i remedy this???

Était-ce utile?

La solution 2

Just answering your question from 2 days ago -

for a_value in percentages:, my turtles heading is going to be: a_value multiplied by 360. My turtle is then going to turn left in that direction, walk off drawing a line, and then going back to the middle of the circle.

That's going to be the end of my loop. When the next loop sets off, my turtle will - turn left in the direction of the next a_value in percentages, it's going to stroll off and draw a line, and then return to the middle.

Basically, my turtle is going to keep doing this for every value in percentages until there's nothing left in the percentages list. Just remember your loop starts off when you do the for command - what your telling python in plain speak is "for each value in percentages, I want you to do the following:" and off it goes executing everything underneath that.

Autres conseils

As I know this is for an assignment, can't just hand over the code for it.

There's several elements in this, hopefully I can point you in the general direction of working on a solution :)

Firstly - pie chart is just a circle with lines in it to represent values. Turtle does have a circle function in it, so boom, pie chart has a its start already.

Secondly, you need to convert those items in "percentages" to a meaningful value. (hint, circle has 360 degrees), which I'm going to call percent_heading just to make this explanation easier.

Thirdly, creating a loop, cause you need the turtle to draw a line for each percent_heading. So for each value in percentages, turn your turtle to percentage_heading, draw a line from the middle of the circle to the outer, and then back to the middle, ready to turn to the next percent_heading.

At the end of this, you should have a circle with a bunch of lines drawn it that represent the percentages.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top