Contents

  1. circle_move_repeat.py
  2. more_range_examples.py
  3. our_solutions.py
  4. range_analysis.py
  5. simple_for.py
  6. sum5.py
  7. using_range.py

circle_move_repeat.py 1/7

[
top][prev][next]
# Move a circle to where the user clicked
# Move three times.
# By CS111

from graphics import *

canvas = GraphWin("Move the Circle", 500, 500)

# create the circle in the upper left
circle = Circle( Point(50,50), 50)
circle.setFill("fuchsia")
circle.draw(canvas)

# put the directions in the top center of the canvas
directions = Text( Point(250, 20), "Click where you want to move the circle to.")
directions.draw(canvas)

for i in range(3):
	# get the point from the user's click
	new_point = canvas.getMouse()
	newX = new_point.getX()
	newY = new_point.getY()
	
	# need to move the circle to that point, but move requires a dx and dy...
	dx = newX - circle.getCenter().getX()
	dy = newY - circle.getCenter().getY()
	
	circle.move(dx, dy)

# wait for the mouse click
canvas.getMouse()
canvas.close()

more_range_examples.py 2/7

[
top][prev][next]
# More range examples
# by 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_solutions.py 3/7

[
top][prev][next]
# Solutions

#
print("for a to generate 1,2,3,4,5")

for i in range(1,6,1):
	print(i)
	
print()
for x in range(1,6):
	print(x)
	
print()
for whatever in range(5):
	print(whatever+1)


print()
print("for b to generate 2, 5, 8, 11")

for myVar in range(2, 14, 3):
	print(myVar)

print()
for i in range(2, 12, 3):
	print(i)
	
	
print()
print("for c to generate ****, ****,****")

for i in range(3):
	print("****")

range_analysis.py 4/7

[
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 5/7

[
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'...")
   
print()

num_repetitions = 5

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


sum5.py 6/7

[
top][prev][next]
# Adding up numbers from the user
# By CSCI111

# initialize the accumulator variable
total = 0

# request that user says how many numbers they want to add up.
# (originally, we just had the user enter 5 numbers.)
numNums = int(input("How many numbers do you want to add up? "))


# loop until we're done
for i in range(numNums):
	number = int(input("What number do you want to add? "))
	# update the accumulator
	total = total + number
	# not needed but helpful in watching our program run
	print("Total so far is...",i,  total)
	
# display the results
print("Your total is", total)

using_range.py 7/7

[
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.