Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

POGIL FOR Loops

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

Looping Structures: FOR Loops

Name: Daniel Portalski Partner: None


Looping Structures: FOR Loops

Learning Objectives
Students will be able to:
Content:
● Explain the difference between ​while loop ​ and a F
​ OR loop
● Explain the syntax of a ​FOR loop
● Explain how to use the ​range()​ function in a F
​ OR loop
● Explain an ​accumulator ​ in a ​FOR loop
Process:
● Write code that includes​ FOR loop
● Write code that uses use ​FOR loops ​within functions

Critical Thinking Questions:


1. Enter and execute the following two Python programs.
WHILE LOOP -- Python Program

FOR LOOP – Python Program

a. What is the output for each program?


The while loop prints my name 20 times
The for loop prints prints my name 20 times

b. Both programs produce the same output. Which code fragment is more concise?

The for loop


Looping Structures: FOR Loops

FYI: ​The Python predefined function -


range()​ - is used to define a series of
numbers and can be used in a FOR loop
to determine the number of times the
loop is executed..
2. Enter and execute the following code fragments and state the output:
a. for x in range(5):
print(x, end=" ")
0 1 2 3 4
b. for x in range(1,5):
print(x, end=" ")
1 2 3 4

c. for x in range(3,20,2):
print(x, end=" ")
3 5 7 9 11 13 15 17 19
d​. numIterations = 6
for x in range(numIterations):
print(x, end=" ")
0 1 2 3 4 5
e​. numIterations = 6
for x in range(1, numIterations+1):
print(x, end=" ")
0 1 2 3 4 5 6

3. After examining the five code fragments in #2, explain how the ​range()​ function works. Include an
explanation of the arguments (inputs).
The range function produces a sequence of numbers over time and produces an output based on that
sequence of numbers. Arguments in the range function change the range, the rate at which the range changes
and how much should be added to the range.

FYI: ​In a FOR loop you can include


a list of values in place of the
range() ​function.
Looping Structures: FOR Loops

4. Enter and execute the following code.


for x in [3,6,9,12,15,18]:
print(x, end=” “)
3 6 9 12 15 18
a. Rewrite this code using the ​range()​ function.

for x in range(3,19,3):
print(x, end=" ")
b. Rewrite this code using a list of values:

for x in range(1,5):
print(x, end=" ")

c. Why would you use the ​range() ​function when you could just list the numbers?
To be more efficient.

5.​ ​We learned about three actions that must occur in every loop:

i) Initialize a variable used in the condition expression


ii) Include the condition expression that causes the loop to end when the
condition is false
iii) Within the loop body, update the variable used in the condition statement

a) Rewrite this FOR loop as a while loop.

for x in range(1, 5):


print(x, end = “ “)

x=1
while x<5:
print(x, end=" ")
x=x+1

b) How are these three actions implemented in the FOR loop? ​This is a bit tricky. Do the best you can
to explain.

​for x in range(1, 5):


print(x, end = “ “)

The x in range(1,5) is the equivalent to the while line and the print is repeated five times.
Looping Structures: FOR Loops

FYI: ​The ​str() ​function converts


what is the parentheses ( ) to a
String.
6. Read through the code and determine what it does.
favorite = input("Enter your favorite ice cream flavor: ")
for x in range(1,5):
print(str(x) + “.”, favorite, end="\t")

a. What do you think the program does


It prints string(x) a number from 1-4, and the favorite ice cream flavor with \t at the end four times

b. Enter and execute the code to determine if you were correct. What does the program
actually do? Provide a detailed explanation.
The program prints a number from 1-4 with a period at the end (1.) and then it prints the inputted ice
cream flavor and makes an indent space.

c. Why is the ​str() ​function needed in the print statement?


You cannot concatenate an integer and a string
7. Complete the arguments in the following range function so that the code prints the even numbers
between 100 and 200 inclusive.
for x in range(100,201,2):
print(x)

8. Complete the arguments in the following range function so that the code prints: 5 4 3 2 1 0.
for x in range(5,-1,-1):
print(x, end=’ ‘)
Looping Structures: FOR Loops

FYI: ​An ​accumulator ​is a variable


that stores the sum of a group of
values.
9. Examine the following code segment.
total = 0
for x in range(5):
number = int(input("Enter a number: "))
total += number
print("The total is:",total)

a. Why is the variable ​total ​initialized to 0 in the first line of code?


Because the initial value of the total is 0​.

b. Explain what the following code does:


number = int(input("Enter a number: "))
It gets a value for the number.

c. Explain what the following code does: ​total += number


It adds the value of the number to the total to get a new total value.

d. How many numbers does the program prompt for?


Five numbers.

e. What is the ​accumulator​ in the code segment?


The total.

10. Is it better to use a ​FOR loop​ when you know the number of times the loop should be executed or when
you do not know (support your answer with an explanation)?
It is better to use a FOR loop when you do know the number of times the loop should be
executed because it is more efficient than using a WHILE loop that would need a counter and it would
take up more space.

11.​ ​Determine if a WHILE loop or a FOR loop is more appropriate for the following
problems. Write the FOR or WHILE loop code snippet

a. Prompt the user for 6 numbers. Calculate the average of the six numbers and print it
to the screen.
Looping Structures: FOR Loops

total = 0

for x in range(6):

number = int(input("Enter a number: "))

total=total+number

print("The average is:",total/6)

b. Prompt the user to enter numbers until the user decides he/she is finished. Calculate the
average of the numbers and print it to the screen.
number=int(input("Enter a number: "))

total=number

counter=1

doAgain="y"

while doAgain=="y":

number = int(input("Enter a number: "))

total=total+number

counter=counter+1

doAgain=input("Do you want to print another number? y/n? ")

print("The average is:",total/counter)

c. Prompt the user for the amount of numbers she/he wants to enter. Prompt the user to enter
those values then find the average and print it to the screen.
amount=int(input("How many numbers do you want to enter: "))
x=0
total=0
while x<amount:
num=int(input("Please enter a number: "))
total=total+num
x=x+1
print("The average is:",total/amount)
Looping Structures: FOR Loops

Application Questions: Use the Python Interpreter to check your work

1. Write a code segment using a FOR loop that prints multiples of 5 from 5 to 500, each one on its
own line.
for x in range(5,501,5):
print(x)

2. Write a FOR loop to print the following:

99 bottles of milk on the wall, 99 bottle of milk! Take 1 down, pass it around, 98 bottle
of milk on the wall.
98 bottles of milk on the wall, 98 bottles of milk! Take 1 down, pass it around, 97
bottles of milk on the wall.

1 bottles of milk on the wall, 1 bottles of milk! Take 1 down, pass it around, 0 bottles
of milk on the wall.

for x in range(99,0,-1):
print(x,"bottles of milk on the wall,",x,"bottle of milk! Take 1 down, pass it
around,",x-1,"bottle of milk on the wall.")

You might also like