Contents

  1. average5.py
  2. more_range_examples.py
  3. new_for.py
  4. range_analysis.py
  5. simple_for.py
  6. sum5.py
  7. using_range.py

average5.py 1/7

[
top][prev][next]
# This program averages numbers supplied by the user.
# By CS111, 01.18.2012

print("This program will average numbers given by the user.")

how_many_numbers = eval(input("How many numbers do you want to average? "))

# initializes the accumulater variable, a running total
sum = 0

for x in range(how_many_numbers):
    userNum = eval(input("Enter a number: "))
    # add the current number to the running total
    sum = sum + userNum

# calculate the average
average = sum/how_many_numbers

# display the result
print("The average is", average)

more_range_examples.py 2/7

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

new_for.py 3/7

[
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, 15, 1.5) ------------"
# Note that range expects integer values
for counter in range(5.5, 15, 1):
    print(counter)

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(10):
   squared = i * i
   print(i , "^2 =", squared)

print(i)

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


simple_for.py 5/7

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

# The "chorus" gets repeated 5 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.py 6/7

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

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

# initializes the accumulater variable, a running total
sum = 0

# repeat 5 times
for x in range(5):
    # prompts the user for the number
    userNum = eval(input("Enter a number: "))
    # add the current number to the running total
    sum = sum + userNum
    # Alternatively: sum += userNum

# display the result
print("The total sum is", sum)

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