This presentation is a part of the COP2271C college level course taught at the Florida Polytechnic University located in Lakeland Florida. The purpose of this course is to introduce Freshmen students to both the process of software development and to the Python language.
The course is one semester in length and meets for 2 hours twice a week. The Instructor is Dr. Jim Anderson.
A video of Dr. Anderson using these slides is available on YouTube at:
https://www.youtube.com/watch?feature=player_embedded&v=ffTdp1FQj_s
1. An Introduction To Software
Development Using Python
Spring Semester, 2015
Class #7:
WHILE Loop
2. How Do You Do Things
Over And Over Again?
• In Python, loop statements repeatedly execute
instructions until a goal has been reached.
• In Python, the while statement implements such a
repetition. It has the form:
while condition :
statement1
statement 2
• As long as the condition remains true, the
statements inside the while statement are executed.
Image Credit: etc.usf.edu
Body
Condition
3. Remember Mr. Bank Account
Interest Rate Problem?
Problem:
You put $10,000 into a bank account that earns 5 percent interest per year.
How many years does it take for the account balance to be double the original?
(over and over & over again)
Some people call this loop an event-controlled loop.
4. How Can I Loop For A Given
Number Of Times?
• You can use a while loop that is controlled by a
counter:
counter = 1 # Initialize the counter.
while counter <= 10 : # Check the counter.
print(counter)
counter = counter + 1 # Update the loop variable
Note: Some people call this loop count-controlled.
Image Credit: www.dreamstime.com
5. Loop Challenge!
• Create a program to print the sum of all odd
numbers between a and b (inclusive).
Image Credit: www.dreamstime.com
6. Infinite Loops
• Def: a loop that runs forever and can be
stopped only by killing the program or
restarting the compute
year = 1
while year <= 20 :
interest = balance * RATE / 100
balance = balance + interest
Image Credit: www.canstockphoto.com
7. The Loop Problem:
“Off By-One Errors”
year = 0
while balance < TARGET :
year = year + 1
interest = balance * RATE / 100
balance = balance + interest
print("The investment doubled after", year, "years.")
• Should year start at 0 or at 1?
• Should you test for balance < TARGET or for
balance <= TARGET?
• It is easy to be off by one in these expressions.
• Think through simple test cases to avoid this type of error.
Image Credit: www.wisegeek.org
8. Sentinel Values
• One common programming task is
to read and process a sequence of
input values.
• You need to have some method of indicating the end of the
sequence.
– 0?
– -1?
• Such a value, which is not an actual input,
but serves as a signal for termination, is called a sentinel
9. Sentinel Value Example:
Average Salary
• Create a program that computes the average of a set
of salary values.
• In our sample program, we will use any negative
value as the sentinel.
• Any negative number can end the loop, but we
prompt for a sentinel of –1 so that the
user need not ponder which negative number to
enter.
Image Credit: www.clipartpanda.com
10. What Do We Use Loops For?
• Sum and Average Value
• Counting Matches
– How many values fulfill a particular condition.
• Prompting Until a Match is Found
• Maximum and Minimum
• Comparing Adjacent Values
Image Credit: www.clipartpanda.com
11. Remember
The Python ATM Machine?
Image Credit: www.canstockphoto.com
Create software that will provide an ATM user
with the proper change for any dollar amount
up to $200.
Example: Run the code for $200 and for $19
12. What’s In Your Python Toolbox?
print() math strings I/O IF/Else elif While
13. What We Covered Today
1. While Loop
2. Infinite Loops
3. Sentinel Values
Image Credit: http://www.tswdj.com/blog/2011/05/17/the-grooms-checklist/
14. What We’ll Be Covering Next Time
1. For Loop
Image Credit: http://merchantblog.thefind.com/2011/01/merchant-newsletter/resolve-to-take-advantage-of-these-5-e-commerce-trends/attachment/crystal-ball-fullsize/
Editor's Notes
New name for the class
I know what this means
Technical professionals are who get hired
This means much more than just having a narrow vertical knowledge of some subject area.
It means that you know how to produce an outcome that I value.
I’m willing to pay you to do that.