Contents

  1. average5.py
  2. more_range_examples.py
  3. range_analysis.py
  4. simple_for.py
  5. sum5_no_loop.py
  6. sum5.py

average5.py 1/6

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

more_range_examples.py 2/6

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

range_analysis.py 3/6

[
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?
# What is the value of i after the loop?
# What happens if i is renamed to x throughout the program?


simple_for.py 4/6

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


for i in range(3):
    doubled = i * 2
    print( i, "*2=", doubled)

sum5_no_loop.py 5/6

[
top][prev][next]
# This program adds up 5 numbers from the user.
# Compare this version to the version that uses a for loop.
# By CS111

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

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


total = num1 + num2 + num3 + num4 + num5

print("The total of the inputted numbers is ", total)

sum5.py 6/6

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

total = 0

# repeat 5 times
for x in range(5):
    # get the input from the user
    num = eval(input("Input number: "))
    # add the user's input to my total
    total = num + total
    # total += num

print("The total of the inputted numbers is ", total)

# To address a question in class,
# you could print num and it would have the last value assigned to num: 
# print(num)

Generated by GNU Enscript 1.6.6.