9. Several Practical Topics

9.1. Modules

7.1 Import

Split the single file of exercise 4.8 into two files: one with all the special turtle functions (SpecialTurtleFunctions.py) and one that uses these functions, listed below:

from SpecialTurtleFunctions import *

def main():
    # Draw a pentagon
    drawLine(100, 0, 31,-95)
    drawLine(31, -95, -81,-59)
    drawLine(-81, -59, -81, 59)
    drawLine(-81, 59, 31, 95)
    drawLine(31, 95, 100, 0)

    # Draw points at the corners of the pentagon
    drawPoint(100, 0)
    drawPoint(31, -95)
    drawPoint(-81, -59)
    drawPoint(-81, 59)
    drawPoint(31, 95)

    # Draw a circle at (0, 0) with radius 80
    drawCircle(0, 0, 81)

    # Draw a rectangle at (0, 0)
    # with width 60 and height 40
    drawRectangle(10, 2, 182, 190)

    # Write text at (-50, -60)
    writeText("Functions make programs shorter !", -120, -120)

    turtle.hideturtle()
    turtle.done()

9.2. Random Numbers

7.2 Random

With the random module we can simulate statistical events, like throwing dice to calculate probabilities.

2a. If you throw a dice many times, what is the expectation value of a throw? (= average over many throws)? Write a little program that simulates throwing a dice and calculates the average throw over 1000 experiments. Hint: for randomly selecting integer numbers you need the randint() function.

2b You have 3 red socks, 5 blue socks and 4 green socks in your drawer. They are not in pairs. If you would pick two socks in the dark, what is the probability that you pick a matching pair (red-red, blue-blue or green-green)? Write a little program that calculates the probability by doing the experiment 1000 times.

Hint 1: Put your ‘socks’ in a list.

Hint 2: After you select the first sock you have to take it out of the list (you cannot wear the same sock on two feet at the same time …)

9.3. File Reading and Writing

TBD

9.4. Timing

TBD

© Copyright 2022, dr. P. Lambooij

last updated: 07-10-2022