Hi, I’m trying to create a barcharts using python turtle for my assignment. So the overall code is about entering student marks, and then I need to make a barcharts based on these students overall marks.
I have trouble trying to figure out, how to exactly enter the values from the entered student marks into the turtle so that the barchart can be created.
All I got from google is this site:
http://interactivepython.org/runestone/static/thinkcspy/Functions/ATurtleBarChart.html
In the site’s example, the data was entered manually instead of from an existing values that was entered by users.
The algorithm I have in mind is:
- Create a function to draw the barchart
- Create a function to insert data to the barchart
- Create a function to combines the two functions together
My current code looks something like this, and I know this code might not make sense at all:
def drawBar(t, height):
‘’’ Get turtle t to draw one bar, of height.’’’
t.begin_fill() # start filling this shape
t.left(90)
t.forward(height)
t.write(str(height))
t.right(90)
t.forward(40)
t.right(90)
t.forward(height)
t.left(90)
t.end_fill() # stop filling this shape
import turtle
def turtleBarChart(allStudentsMarks):
‘’'data for turtle
:param allStudentsMarks:
:return:
'''
OverallMarks = {}
for keyStudentName,marks in allStudentsMarks.items():
OverallMarks [keyStudentName] = sum(marks)
value = OverallMarks[keyStudentName, marks]
maxheight = max(int(value))
numbars = len(int(value))
border = 10
wn = turtle.Screen() # Set up the window and its attributes
wn.setworldcoordinates(0-border, 0-border, 40*numbars+border, maxheight+border)
wn.bgcolor("lightgreen")
tess = turtle.Turtle() # create tess and set some attributes
tess.color("blue")
tess.fillcolor("red")
tess.pensize(3)
def drawBarChart(allStudentsDetails):
for keyStudentName in OverallMarks:
drawBar(tess, keyStudentName)
wn.exitonclick()