An Introduction To Python - WHILE Loop

14
An Introduction To Software Development Using Python Spring Semester, 2015 Class #7: WHILE Loop

Transcript of An Introduction To Python - WHILE Loop

Page 1: An Introduction To  Python - WHILE Loop

An Introduction To Software

Development Using Python

Spring Semester, 2015

Class #7:

WHILE Loop

Page 2: An Introduction To  Python - WHILE Loop

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 : statement1statement 2

• As long as the condition remains true, the statements inside the while statement are executed.

Image Credit: etc.usf.edu

Body

Condition

Page 3: An Introduction To  Python - WHILE Loop

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.

Page 4: An Introduction To  Python - WHILE Loop

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

Page 5: An Introduction To  Python - WHILE Loop

Loop Challenge!

• Create a program to print the sum of all odd numbers between a and b (inclusive).

Image Credit: www.dreamstime.com

Page 6: An Introduction To  Python - WHILE Loop

Infinite Loops

• Def: a loop that runs forever and can be stopped only by killing the program or restarting the compute

year = 1while year <= 20 :

interest = balance * RATE / 100balance = balance + interest

Image Credit: www.canstockphoto.com

Page 7: An Introduction To  Python - WHILE Loop

The Loop Problem:“Off By-One Errors”

year = 0while balance < TARGET :

year = year + 1interest = balance * RATE / 100balance = 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

Page 8: An Introduction To  Python - WHILE Loop

Sentinel Values

• One common programming task isto 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

Page 9: An Introduction To  Python - WHILE Loop

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 theuser need not ponder which negative number to enter.

Image Credit: www.clipartpanda.com

Page 10: An Introduction To  Python - WHILE Loop

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

Page 11: An Introduction To  Python - WHILE Loop

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

Page 12: An Introduction To  Python - WHILE Loop

What’s In Your Python Toolbox?

print() math strings I/O IF/Else elif While

Page 13: An Introduction To  Python - WHILE Loop

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/

Page 14: An Introduction To  Python - WHILE Loop

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/