CSC 1010 Lecture 6 What do we know so far? Class lecture, lab, - - PDF document

csc 1010 lecture 6
SMART_READER_LITE
LIVE PREVIEW

CSC 1010 Lecture 6 What do we know so far? Class lecture, lab, - - PDF document

CSC 1010 Lecture 6 What do we know so far? Class lecture, lab, Rephactor, Quick Checks, R&R, easter eggs Solve problems, computers useful, user vs. programmer CSC 1010 Programming for All Sequence of instructions,


slide-1
SLIDE 1

CSC 1010 Lecture 6 1

CSC 1010 Programming for All

Lecture 6 Loops, Functions & Turtles

What do we know so far?

  • Class – lecture, lab, Rephactor, Quick Checks, R&R, easter eggs
  • Solve problems, computers useful, user vs. programmer
  • Sequence of instructions, algorithm is step‐by‐step
  • Python is 3rd most popular language, core principles
  • Syntax, runtime, & logic errors, testing & debugging
  • Hardware vs. software
  • control flow – step‐by‐step, function call, conditional, loop
  • IDLE shell, editor, install Python, Hello World
  • Intrepreter, compiler, Python Standard Library
  • Variables, assignment, numeric expr., precedence
  • Print function, Strings, concatenation, indexes, in, *
  • Interactive programs, if, if‐else, if‐elif‐else, int, float
  • Boolean expressions: ==, !‐, <, <=, >, >=, not, and, or
  • Input function, comparing strings, programming conventions
  • variable & function names lowercase, CONSTANTS, indent
  • while, for, range, augmented assignments, palindromes
  • Turtle Graphics, forward, left, right, pensize, pencolor, dot, circle
  • goto, penup, pendown, fillcolor, begin_fill, end_fill, speed
  • calling & defining functions, import, parameters vs. arguments, return
  • positional args, default args, variable args, keyword args, local variables
  • string methods, replace, method vs. function, built‐in & external functions

2

Loops & Turtle Graphics

Loops are ideal for repeating the same thing over and over. This program draws the 5 points of a star:

3

i m por t t ur t l e f or count i n r ange( 5) : t ur t l e. f or war d( 100) t ur t l e. r i ght ( 144) t ur t l e. hi det ur t l e( )

To make the result clean, hide the turtle using hideturtle.

Loops & Turtle Graphics

Repetition allows you to create more complex shapes easily:

4

i m por t t ur t l e t ur t l e. pencol or ( " gr een" ) f or i i n r ange( 41) : t ur t l e. f or war d( 100) t ur t l e. r i ght ( 123) t ur t l e. hi det ur t l e( )

Experimentation is the best way to discover new designs.

Other Design Ideas

5

Spirals

See the textbook for a variety of approaches and code examples

Other Design Ideas

6

Flowers

f or count 1 i n r ange( 10) : f or count 2 i n r ange( 2) : t ur t l e. f or war d( 100) t ur t l e. r i ght ( 60) t ur t l e. f or war d( 100) t ur t l e. r i ght ( 120) t ur t l e. r i ght ( 36)

The key piece is a nested loop… a for loop inside another for loop

1 2 3 4 5 6

slide-2
SLIDE 2

CSC 1010 Lecture 6 2

Using Randomness

7

i m por t t ur t l e i m por t r andom f or i i n r ange( 20) : i f ( r andom . choi ce( [ Tr ue, Fal se] ) ) : t ur t l e. r i ght ( 30) el se: t ur t l e. l ef t ( 30) t ur t l e. f or war d( 40) t ur t l e. st am p( )

Using random values can make computer generated graphics more interesting and unique. Explore methods in the random module.

Turtle Graphic Functions

8

def dr aw_t wo_ci r cl es( r adi us) : t ur t l e. hi det ur t l e( ) t ur t l e. ci r cl e( r adi us) t ur t l e. penup( ) t ur t l e. got o( r adi us * 2, 0) t ur t l e. pendown( ) t ur t l e. ci r cl e( r adi us)

A function encapsulates a reusable shape or design. The goto function moves the turtle to a X, Y position

Honeycomb Example

9

def dr aw_hexagon( ) : f or count i n r ange( 6) : t ur t l e. f or war d( 50) t ur t l e. l ef t ( 60)

Step 1: Create reusable functions that draw an individual shape

Honeycomb Example

10

# Dr aws a honeycom b shape. def dr aw_honeycom b( ) : f or dr aw_honeycom b i n r ange( 6) : dr aw_hexagon( ) t ur t l e. f or war d( 50) t ur t l e. r i ght ( 60)

Step 2: Create a function that repeatedly calls the first function

Honeycomb Example

11

# M ai n pr ogr am t ur t l e. penup( ) t ur t l e. got o( - 35, 50) t ur t l e. pendown( ) t ur t l e. pencol or ( ' gol d' ) t ur t l e. pensi ze( 7) dr aw_honeycom b( ) t ur t l e. hi det ur t l e( )

Step 3: Set color and pen size, and call draw_hexagon.

How to Approach Creating Designs

  • Look for examples to inspire new ideas.
  • Invent ideas of your own.
  • Reuse ideas that you see with your own wrinkles.
  • Think Functions! Break up code into smaller units.
  • Think Loops! Use for or while loops to repeat things.
  • Break up your idea into smaller pieces.
  • Create a function for each piece.

12

7 8 9 10 11 12

slide-3
SLIDE 3

CSC 1010 Lecture 6 3

Check on the comprehensive Turtle Graphics Reference topic

  • Creating Turtles
  • Basic Movement
  • Pen Control
  • Dots and Circles
  • Filling Shapes
  • Turtle Appearance
  • Stamps
  • Getting the Turtle's State
  • Miscellaneous
  • Environment Control

Turtle Graphics Reference

13

Random Numbers

A random number generator is an object that produces a stream

  • f pseudorandom numbers

They are based on a seed value that factors into a set of calculations To the user, they certainly appear random The mechanism for generating random numbers in Python is the random module They are called pseudorandom because they are calculated using a complex formula… so they only appear to be random!

14

Random Numbers

The random module has several methods for generating random numbers The randrange method accepts an argument N and returns an integer in the range 0 to N‐1

15

i m por t r andom num = r andom . r andr ange( 10)

The variable num now contains a single integer between 0 and 9, inclusive There's an equal probability of getting any value in that range

Random Numbers

The arguments to randrange can also determine the full range

  • f values, similar to the range function

The first argument is the start of the range, while the second argument is 1 greater than the end of the range.

16

i m por t r andom num = r andom . r andr ange( 1, 51)

This call to randrange returns a value between 1 and 50

Random Numbers

When there is one argument Y, values go from 0 to Y – 1. With two arguments, X and Y, values go from X to Y – 1.

17

Expression Range r andom . r andr ange( 100) 0 t o 99 r andom . r andr ange( 256) 0 t o 255 r andom . r andr ange( 1, 7) 1 t o 6 r andom . r andr ange( 100, 120) 100 t o 119 r andom . r andr ange( - 10, 40)

  • 10 t o 39

r andom . r andr ange( - 50, - 40)

  • 50 t o - 41

There are many other functions in the random module to generate random values and choices, and to shuffle lists.

Random Numbers

The seed value (an integer) can be set for a random object using the seed method

18

i m por t r andom r andom . seed( 546721)

The seed value determines exactly the stream of numbers that will be produced If you don't call seed, the current time (in milliseconds) is automatically used instead

13 14 15 16 17 18