Topic 5 - Repetition Flashcards
How do we perform a task several times?
We use loops!
What are the two types of loops that python uses? What is the difference between them?
Python uses while loops and for loops.
While loops and for loops both allow a statement that only appears once in the program to execute multiple times. While loops execute as long as something is true in the statement, and for loops execute once for each value in a collection of values.
The difference is that they use different mechanisms to determine how many times something is run.
How does a while loop work? What is the formatting for it? What do the statements below need to be able to do?
A while loop executes as long as a condition is true. It appears like an if statement. You write while and then a condition, and if that condition is true the statements which are indented below (the body) will run.
The statements below the while condition need to be able to change one of the values being tested in the condition, or else the loop will just continue running forever because the while condition will always be true.
For example, write a program that computes the average of several numbers, using a while loop.
Sum the numbers:
What steps do we have to take to do this?
Sum all of the numbers, count the number of values that were summed, and divide by the amount of numbers summed.
To make use of the loop, instead of adding all numbers at once we can add one number at a time to the existing sum! Then we can have a certain number that if added actually ends the loop. In this case that would be zero because adding zero does nothing to the average so we can use it to indicate the end of the loop instead.
So write this code:
Num = float(input(“Enter a number (0 to quit): “))
#If the number does not equal zero then it needs to be included in the average:
While num !=0:
Total = total + num
#This adds this new non-zero number to the existing total to produce a new total.
However, this will not run because the initial total value is undefined! Therefore, we have to initialize the total and count (number of numbers added) numbers to make this code work. So try again:
Num = float(input(“Enter a number (0 to quit): “))
#Initialize total and count to zero:
Total = 0
Count = 0
While num !=0:
Total = total + num
#This adds this new non-zero number to the existing total to produce a new total. Count = count + 1 #Read the next input value: Num = float(input(“Enter a number (0 to quit): “)) If count > 0: #We cant divide by zero when computing the average. So count must be > 0. #Compute the average: Avg = total / count #Display the result: Print(“the average of those values is”, avg) Else: Print(“An average cannot be displayed because no values were entered.”) #This else statement will only run if everything else fails, meaning that count was not greater then zero and the only number entered was zero.
We have to have two places to enter numbers and then this loop will just keep repeating. Can’t just do the one place because you cant compute an average with just one value.
A while loop is a ____ _______ loop. It’s general form is:
If the loop is initially false then we…..
A while loop is a pre-tested loop. This means the condition is tested before the loop executes for the first time. So if the condition is false, then the loop will not execute at all.
It’s general form is:
while condition:
statement(s)
If the loop is initially false then we skip right over the body of the loop. It doesn’t start running and then stop because it is PRE-TESTED!
What is the body of the loop? What is the loop condition?
The body of the loop is a simple or compound statement that is repeated as long as the condition is true.
The loop condition is a Boolean expression that is tested to determine if the loop will continue executing.
What do these three terms mean as loop terminology?
Initialization:
Termination:
Pre-tested loop:
Post-tested loop:
Infinite loop:
Is python a pre-tested loop or post-tested loop program?
Initialization: The process of placing starting values in variables before the loop (so that variables are defined if trying to add to them within the loop — like how we initialized the variable “total” for the computing the average loop).
Termination: This is when the loop stops being executed due to some value that was entered that makes the Boolean condition false.
Pre-tested loop: Any loop where the loop condition is checked before the loop executes for the first time, meaning it will not run if false — this is what a while loop does.
Post-tested loop: Any loop where the condition is not checked until the loop has executed once, so it has to run at least one (or more) times. An example of this is the division algorithm, because you have to divide a by the base until you get zero, so you have to divide at least once before the loop stops (even if you are starting with zero, because the program relies on the quotient being zero in the end to stop, it doesn’t care if the input is zero).
An infinite loop is a loop that never terminates, and this is usually a bug.
Python uses only pre-tested loops, so any post tested loops have to be manipulated to turn them into pre-tested loops.
Example: Use a loop to compute the n factorial…
Read n from the user:
n = int(input(“Enter an integer: “))
org_n = n
result = 1 (don’t want to multiply by zero)
while n >= 1:
#multiply the result so far by the current value of n:
result = result * n
#Decrease n
n = n - 1
#Display the value of n factorial (n!):
Print(“The value of”, n, “factorial is”, result)
This works because you start with the input, and when n is greater then 1 you multiply it by the current result which is 1 (don’t want to multiply by zero). Then you decrease n by one and run the loop again, multiplying the current result with this new n value, and you repeat that loop until n < 1, and this is when the loop will be exited.
So essentially, as long as the condition in the while loop is true, it will run everything in its body from top to bottom, and then will go back to the top and keep running with the new values until it gets to a value that makes the condition false.