Contents
- ./more_range_examples.py
- ./our_solutions.py
- ./range_analysis.py
- ./simple_for.py
- ./sum5.py
- ./sum_nums.py
- ./using_range.py
./more_range_examples.py 1/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)
./our_solutions.py 2/7
[top][prev][next]
# Solutions to problems on slide
# by CSCI111
print("for a to display 1,2,3,4,5")
for i in range(1, 6):
print(i)
print()
for x in range(5):
print(x+1)
print()
print("for b to display 2, 5, 8, 11")
for i in range(2, 12, 3):
print(i)
print()
for var in range(2, 14, 3):
print(var)
print()
print("for c to display ****, ****,****")
for i in range(3):
print("****")
./range_analysis.py 3/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("Horray!")
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/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 5/7
[top][prev][next]
# Adding up 5 numbers from the user
# By CSCI111
print("This program adds up 5 numbers given by you, the user.")
print()
accumulator = 0
# repeat 5 times
for i in range(5):
# get input from user
user_input = eval(input("Enter the number you want to add: "))
# add user input to our current total (accumulator)
accumulator = accumulator + user_input
print("Your total is", accumulator)
./sum_nums.py 6/7
[top][prev][next]
# Adding up numbers from the user
# By CSCI111
print("This program adds up numbers given by you, the user")
print()
#numNums = 5
numNums = int(input("How many numbers do you want to add up? "))
accumulator = 0
# repeat 5 times
for i in range(numNums):
# get input from user
user_input = eval(input("The number you want to add: "))
# add user input to our current total (accumulator)
accumulator = accumulator + user_input
print("Your total is", accumulator)
./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,2) -------------")
for x in range(1, 10, 2):
print(x)
# What happens if step is negative?
# What happens if stop < start?
Generated by GNU Enscript 1.6.5.90.