Lesson 19: Bar charts with Turtle

posted Mar 10, 2020, 8:02 AM by Attila Lengyel
import random
results = [0,0,0,0,0,0,0,0,0,0,0,0]

for x in range(10000):
dice = random.randint(1,6)
dice2 = random.randint(1,6)
results[dice + dice2 -1] = results[dice + dice2-1] + 1

import turtle as t
nike = t.Turtle()
nike.penup()
nike.setposition(-200,-200)
nike.pendown()
nike.fd(400)
nike.penup()
nike.setposition(-200,-200)
nike.pendown()
nike.left(90)
nike.fd(400)
x_start = -230
for r in results:
x_start = x_start + 40
nike.penup()
nike.setposition(x_start,-200)
nike.pendown()
height = (400 * r)/2000
nike.begin_fill()
nike.fd(height)
nike.write(r)
nike.right(90)
nike.fd(20)
nike.right(90)
nike.fd(height)
nike.end_fill()
nike.left(180)


Comments