Go from for loops and while loops to functions

The main idea behind this section. Reusing instructions (programmatic operations and instructions, storing instruction on files(scripts), and reusing functions)

How to run this section? Build up the need of using multipline commands, to source files, to functions (Demonstrate by example what reusability means in this section, this is where functions come into place)Reflect on the notion of objects

In order to work with python built in IDLE do py -3 -m idlelib

Excercise, drawing polygons with turtle

This excercise also aims to show the

import turtle
from polygon import sort_points, get_angle
## Explain differences
# from turtle import * 

## Explain how to use the help()
# help(turtle)

## Excercise: 
# Do this first manually
# turtle.shape("turtle")
# turtle.forward(100)
# turtle.setheading(90)
# turtle.forward(100)
# turtle.setheading(180)
# turtle.forward(100)
# turtle.setheading(270)
# turtle.forward(100)

## Do it then with a for loop
## Explain the need for a foor loop
# length = 100
# directions = [0,90,180,270]
# for dir in directions:
#     turtle.setheading(dir)
#     turtle.forward(length)

## Example of a function
## triangle = [[a_side, C_angle], [b_side, A_angle], [c_side, B_angle]]
def draw_triangle(triangle):
    for entry in triangle:
        print(entry[0])
        turtle.forward(entry[0])
        turtle.left(entry[1])



## Excercise: 
## Building a polygon with a better data model
def draw_polygon(coordinates):
    '''
    By doing myself this excercise I figured out problems to solve
    like for instance the if statement at the end of the function
    '''
    angle_list = []
    for point in coordinates:
        angle_list.append(get_angle(point))

    print(angle_list)

    turtle.penup()
    count = 0
    for coord in coordinates:
        count += 1 
        turtle.goto(coord[0], coord[1])
        turtle.pendown()
        # turtle.circle(5)
        # turtle.penup()
        if(count == len(coordinates)):
            turtle.goto(coordinates[0][0], coordinates[0][1])


def draw_better(coordinates):
    coordinates = sort_points(coordinates)
    draw_polygon(coordinates)

## Another triangle data model
## A polygon model
##   Latitude,Longitude
##   [[51.984338,4.136685],
##   [51.852919,5.290831],
##   [52.861383,5.024489]]
draw_coordinates()
polygon = [[300,-150],[20,30],[100,200],[-50,180], [100,80]]
## Explain this example first and why is it so bad
# draw_triangle([[100,100],[264,19],[300,60]])
# draw_triangle([[100,100],[264,180-19],[300,60]])

## Explain these example and the data model and why is better.
# draw_polygon([[20,30],[100,200],[-50,180],[300,-150]])
draw_polygon(polygon)
# print(polygon)
# print(sort_points(polygon))
# draw_polygon([[-50,180],[100,200],[300,-150],[20,30]])

turtle.exitonclick()
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-3-f3c6770433f9> in <module>
     71 ##   [51.852919,5.290831],
     72 ##   [52.861383,5.024489]]
---> 73 draw_coordinates()
     74 polygon = [[300,-150],[20,30],[100,200],[-50,180], [100,80]]
     75 ## Explain this example first and why is it so bad

NameError: name 'draw_coordinates' is not defined
import turtle
def draw_coordinates():
    turtle.goto(0,300)
    turtle.goto(0,-300)
    turtle.penup()
    turtle.goto(0,0)
    turtle.pendown()
    turtle.goto(300,0)
    turtle.goto(-300,0)
import turtle
from polygon import *
turtle.speed(2)
def draw_polygon(coordinates):

    # To properly draw the polygon we need to center the centroid
    # Into the turtle coordinate system
    center = centroid(coordinates)
    for idx, point in enumerate(coordinates):
        coordinates[idx][0] = coordinates[idx][0] - center[0]
        coordinates[idx][1] = coordinates[idx][1] - center[1]

    coordinates = sort_points(coordinates)
    print(list_ordered_angles(coordinates))
    print(sort_points(coordinates))

    turtle.penup()
    count = 0
    for coord in coordinates:
        count += 1 
        turtle.goto(coord[0], coord[1])
        turtle.pendown()
        
        # Close the polygon by going back to the first point in the list
        if(count == len(coordinates)):
            turtle.goto(coordinates[0][0], coordinates[0][1])
    
draw_coordinates()

## Examples of polygons
# polygon = [[-50,180],[20,30],[100,200],[300,-150], [400,100]] # Working
# polygon = [[50,10],[30,20],[10,50], [40,60]] # Working
# polygon = [[-50,180],[20,30],[100,200],[300,-150], [400,100],[300,300]] # Working
polygon = [[-50,180],[20,30],[100,200],[300,-150], [400,100],[300,300],[100,30],[-400,-200],[70,55], [30,40],[12,13]]
# polygon = [[-100,80],[200,150],[140,-30],[-60,-80]]
# polygon = [[300,-150],[20,30],[100,200],[-50,180], [100,80]]



draw_polygon(polygon)
turtle.exitonclick()
---------------------------------------------------------------------------
Terminator                                Traceback (most recent call last)
<ipython-input-7-3fe3c9da807c> in <module>
      1 import turtle
      2 from polygon import *
----> 3 turtle.speed(2)
      4 def draw_polygon(coordinates):
      5 

~\Anaconda3\lib\turtle.py in speed(speed)

Terminator: 

Excercise, combining functions and object orientation

  • Calculate the total population in the dataset

  • Calculate the biggest city in the dataset

  • Make a script file with a function where you calculate to reuse this calculation.