#!/usr/bin/env python # coding: utf-8 # In[6]: # couple more nested for loops WIDTH=8 HEIGHT=15 for row in range(HEIGHT): for col in range(WIDTH): print("*",end='') print() for row in range(HEIGHT): for col in range(row): print("*",end='') print() # In[7]: # Section 4.8 -- turtle again #first turtle program import turtle import time #magic numbers NUM_CIRCLES=10 START_RADIUS=20 OFFSET=10 ANIMATION_SPEED=1 #set up the turtle turtle.speed(ANIMATION_SPEED) turtle.showturtle() time.sleep(5) #turtle.hideturtle() radius=START_RADIUS for count in range(NUM_CIRCLES): turtle.circle(radius) x=turtle.xcor() y=turtle.ycor() turtle.penup() turtle.goto(x,y-OFFSET) turtle.pendown() radius+=OFFSET time.sleep(15) # In[8]: # second turtle program import turtle import time #magic numbers NUM_CIRCLES=100 RADIUS=100 ANGLE=7 ANIMATION_SPEED=0 #set up the turtle turtle.speed(ANIMATION_SPEED) turtle.hideturtle() # Draw some circles, with turtle # tilted by ANGLE degrees after each # circle is drawn for x in range(NUM_CIRCLES): turtle.circle(RADIUS) turtle.left(ANGLE) time.sleep(15) # In[ ]: # Section 5.1 -- Functions # idea: to "chunk" a group of statements that # exist in a program for doing a specific task # # Think: a "real world" pay program # -- get pay rate <-- # -- get hours worked <-- # -- calculate gross pay <-- each could be a function # -- overtime pay <-- # -- withholdings and benefits <-- # -- net pay <-- # # Benefits of chunking like this: # -- simpler code # -- can reuse code # -- faster development # -- teamwork facilitation # # Two kinds # 1. "void" functions -- execute statements but return nothing # ex. turtle.circle(5) # 2. "value-returning" -- execute statements AND returns a value # ex. print("abc") returns abc; float("123") returns 123.0