Contents

  1. more_horizontal_lines_alternative.py
  2. more_horizontal_lines.py
  3. more_range_examples.py
  4. our_for_examples.py
  5. range_analysis.py
  6. simple_for.py
  7. tictactoe_withfor.py
  8. using_range.py

more_horizontal_lines_alternative.py 1/8

[
top][prev][next]
# Displaying 5 horizontal lines.  The first one is
# 1/3rd down the screen.  The four remaining are each
# 20 px down the screen.
# By CSCI111

from graphics import *

# create the window
win = GraphWin("Horizontal Lines", 200, 200)

# make horizontal line
horizPoint1 = Point(0, 200/3)
horizPoint2 = Point(200, 200/3)
horizLine1 = Line(horizPoint1, horizPoint2)
horizLine1.setWidth(3)
horizLine1.setOutline("purple")
horizLine1.draw(win)

win.getMouse()

# draw four more identical, horizontal lines, 20 px apart
for iteration in [ 1, 2, 3, 4 ]:
    print(iteration) # what will this display?
    # remove the print statement after we understand what this is doing.
    # what should we do in the loop body?
    # clone the line
    horizLine2 = horizLine1.clone()
    # move it down 20 px
    horizLine2.move(0,20)
    # draw it
    horizLine2.draw(win)
    
    # make the thing that we're going to clone be the current clone
    horizLine1 = horizLine2
    
print("We're done.")
win.getMouse()

more_horizontal_lines.py 2/8

[
top][prev][next]
# Displaying 5 horizontal lines.  The first one is
# 1/3rd down the screen.  The four remaining are each
# 20 px down the screen.
# By CSCI111

from graphics import *

# create the window
win = GraphWin("Horizontal Lines", 200, 200)

# make horizontal line
horizPoint1 = Point(0, 200/3)
horizPoint2 = Point(200, 200/3)
horizLine1 = Line(horizPoint1, horizPoint2)
horizLine1.setWidth(3)
horizLine1.setOutline("purple")
horizLine1.draw(win)

win.getMouse()

# draw four more identical, horizontal lines, 20 px apart
for iteration in [ 1, 2, 3, 4 ]:
    print(iteration) # what will this display?
    # remove the print statement after we understand what this is doing.
    # what should we do in the loop body?
    # clone the line
    horizLine2 = horizLine1.clone()
    # move it down 20 px
    horizLine2.move(0,20*iteration)
    # draw it
    horizLine2.draw(win)
    print("Drew one line.")
    
print("We're done.")
win.getMouse()

more_range_examples.py 3/8

[
top][prev][next]
# More range examples
# Sara Sprenkle

print("-------------- range(1, 15, 3) ------------")
for a in range(1,15,3):
    print(a)

print("-------------- range(5, -15, -5) ------------")
for b in range(5, -15, -5):
    print(b)

# Demonstrate these after handout ... 
    
# Won't display anything
print("-------------- range(5, -15, 5) ------------")
for counter in range(5, -15, 5):
    print(counter)

# Won't display anything
print("-------------- range(-5, 15, -5) ------------")
for counter in range(-5, 15, -5):
    print(counter)

print("-------------- range(5.5, 15, 1.5) ------------")
# Note that range expects integer values
for counter in range(5.5, 15, 1):
    print(counter)

our_for_examples.py 4/8

[
top][prev][next]
# Solutions to our examples of using the for loop
# CSCI111

print("First Solution")
for x in range(1, 6, 1):
    print(x)

# Alternative
print("Second Solution")
for x in range(1, 6):
    print(x)
    
# Alternative
print("Third Solution")
for x in range(5):
    print(x+1)
    
print("Solution for problem b")
# alternatively, stop could be 12 or 13
for anything in range(2, 14, 3):
    print(anything)
    
print("Solution for problem c")
stars = "****"
for i in range(3):
    print(stars)

range_analysis.py 5/8

[
top][prev][next]
# Example of for loops using range
# by Sara Sprenkle

# Question: what does range do?
for i in range(5):
   print(i)

print("After the loop:", i)

# QUESTION FOR CLASS:
# How is i changing each time through the loop?

# What happens if you change the variable from
#	i to some other variable name?

simple_for.py 6/8

[
top][prev][next]
# Examples of for loops using range
# by Sara Sprenkle

# The "chorus" gets repeated 3 times
for i in range(3):
    print("You say 'hello'")
    print("And, I say 'goodbye'...")

    
num_repetitions = 5
   
print()

# for loop with only one statement that gets repeated
for x in range(num_repetitions): print("Repeat the chorus!")


tictactoe_withfor.py 7/8

[
top][prev][next]
# Create full-size tic-tac-toe board.
# Adds a for loop at the end of the program to help
# us understand how for loops work.
# By CSCI111

from graphics import *

# create the window
win = GraphWin("Tic-Tac-Toe Board", 200, 200)

# make vertical lines
vertPoint1 = Point(200/3, 0)
vertPoint2 = Point(200/3, 200)
vertLine1 = Line(vertPoint1, vertPoint2)
vertLine1.setWidth(3)
vertLine1.setOutline("purple")
vertLine1.draw(win)

vertPoint1 = Point(200/3*2, 0)
vertPoint2 = Point(200/3*2, 200)
vertLine2 = Line(vertPoint1, vertPoint2)
vertLine2.setWidth(3)
vertLine2.setOutline("purple")
vertLine2.draw(win)

# make horizontal lines
horizPoint1 = Point(0, 200/3)
horizPoint2 = Point(200, 200/3)
horizLine1 = Line(horizPoint1, horizPoint2)
horizLine1.setWidth(3)
horizLine1.setOutline("purple")
horizLine1.draw(win)

horizPoint1 = Point(0, 2*200/3)
horizPoint2 = Point(200, 2*200/3)
horizLine2 = Line(horizPoint1, horizPoint2)
horizLine2.setWidth(3)
horizLine2.setOutline("purple")
horizLine2.draw(win)

win.getMouse()

# Run the program.  What is happening in this code?
for aLine in [ vertLine1, vertLine2, horizLine1, horizLine2 ]:
    print("before:", aLine)
    aLine.move(20, 20)
    print("after:", aLine)

win.getMouse()

using_range.py 8/8

[
top][prev][next]
# Examples of using range, with different numbers of parameters
# by Sara Sprenkle
#

print("------------ range(10) ------------")
for x in range(10):
    print(x)

print("----------- range(5,10) -----------")
for y in range(5, 10):
    print(y)

print("----------- range(1,10,1) -------------")
for x in range(1, 10, 1):
    print(x)

    
# What happens if step is negative?
# What happens if stop < start?

Generated by GNU Enscript 1.6.6.