#!/usr/bin/env python # coding: utf-8 # In[1]: # Section 4.8 -- Turtle with loops import turtle import time #magic numbers NUM_CIRCLES=30 START_RADIUS=5 OFFSET=15 ANIMATION_SPEED=1 #setup the turtle turtle.speed(ANIMATION_SPEED) turtle.hideturtle() radius=START_RADIUS for circle in range(NUM_CIRCLES): turtle.circle(radius) radius+=OFFSET turtle.penup() turtle.goto(turtle.xcor(),turtle.ycor()-OFFSET) turtle.pendown() time.sleep(15) # In[2]: import turtle import time #magic numbers NUM_CIRCLES=200 RADIUS=70 ANGLE_CHANGE=7 ANIMATION_SPEED=0 turtle.hideturtle() turtle.speed(ANIMATION_SPEED) for circle in range(NUM_CIRCLES): turtle.circle(RADIUS) turtle.left(ANGLE_CHANGE) time.sleep(15) # In[5]: # Section 5.1 -- Functions # idea of a function: group of statements that exist in a # program for doing a specific task # # Real-world example: pay calculator <-- each # - consider pay rate <-- of # - hours worked <-- these # - overtime pay <-- can # - benefits and tax withholdings <-- be # - local, county, state, federal taxes <-- a # - from all of this -- calculating the net income <-- function # # Benefits: # - simpler code # - reuse code # - better testing # - faster development # - facilitate teamwork # # Two kinds # 1. "void" functions -- execute statements and nothing else # ex. turtle.hideturtle() # 2. "value-returning" functions -- execute statements and # also returns a value to the user to use for something # ex. print, input, float() # # # # Section 5.2 Defining and Calling Void Function # Function names --- same rules as variable names (generally use verbs) # # Syntax # def functionname(): # stmt # stmt # etc # Example def message(): print('Hello!') print('World!') # notice -- merely defining a function does nothing # you must "call" it for anything to happen # we now have the command "message" message() # In[ ]: