Contents

  1. more_range_examples.py
  2. our_for_examples.py
  3. range_analysis.py
  4. simple_for.py
  5. sum5_advanced.py
  6. sum5_no_loop.py
  7. sum5.py
  8. using_range.py

more_range_examples.py 1/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 2/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")
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 3/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 4/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!")


sum5_advanced.py 5/8

[
top][prev][next]
# This program adds up 5 numbers from the user.
# By CS111

print("This program will add up 5 numbers given by the user.")
print()

yourSum = 0

for whichNum in range(5):
    yourNum = float(input("Enter number " + str(whichNum+1) + ": "))
    yourSum = yourSum + yourNum # alternatively, yourSum += yourNum
    #print("Checking our subtotal:", yourSum)

print("Your total is", yourSum)

sum5_no_loop.py 6/8

[
top][prev][next]
# Representative of the way to implement sum5.py before today's lecture.
# What happens if I asked you to add up 15 numbers... 
# Or 100 numbers intead?  What would writing that code be like?

print("This program will total 5 numbers entered by you!")

num1 = input("Enter the first number: ")
num2 = input("Enter the second number: ")
num3 = input("Enter the third number: ")
num4 = input("Enter the fourth number: ")
num5 = input("Enter the fifth number: ")

total = num1 + num2 + num3 + num4 + num5
print("The total of these numbers is", total)

sum5.py 7/8

[
top][prev][next]
# This program adds up 5 numbers from the user.
# By CS111

print("This program will add up 5 numbers given by the user.")
print()

yourSum = 0 # before we start, the current total is 0

for whichNum in range(5):
    yourNum = float(input("Enter your number: "))
    yourSum = yourSum + yourNum # alternatively, yourSum += yourNum
    #print("Checking our subtotal:", yourSum)

print("Your total is", yourSum)

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.