18
An Introduction To Software Development Using Python Spring Semester, 2015 Class #6: Nested Branches, Multiple Alternatives

An Introduction To Python - Nested Branches, Multiple Alternatives

Embed Size (px)

Citation preview

Page 1: An Introduction To Python - Nested Branches, Multiple Alternatives

An Introduction To Software

Development Using Python

Spring Semester, 2015

Class #6:

Nested Branches,

Multiple Alternatives

Page 2: An Introduction To Python - Nested Branches, Multiple Alternatives

Sample “IF” Problem

In a scheduling program, we want to check whether two

appointments overlap. For simplicity, appointments start at a

full hour, and we use military time (with hours 0–24).

Image Credit: www.clipartpanda.com

Page 3: An Introduction To Python - Nested Branches, Multiple Alternatives

“Hand Tracing”

• Hand Tracking is a technique that you can use in order to determine if your program is working correctly.

• Mentally execute your program’s statements and keep track of the value of each variable.

Image Credit: www.clipartbest.com

Page 4: An Introduction To Python - Nested Branches, Multiple Alternatives

Example: Let’s Talk About TaxesFederal Tax Rate Schedule

Image Credit: www.clipartpanda.com

Page 5: An Introduction To Python - Nested Branches, Multiple Alternatives

Hand Tracing The Tax Program

Tax 1 Tax 2 Income MaritalStatus

80000 m

Page 6: An Introduction To Python - Nested Branches, Multiple Alternatives

Hand Tracing The Tax Program

Tax 1 Tax 2 Income MaritalStatus

0 0 8000 m

Page 7: An Introduction To Python - Nested Branches, Multiple Alternatives

Hand Tracing The Tax Program

Tax 1 Tax 2 Income MaritalStatus

0 0 8000 m

6400 4000

XXX XXX

Page 8: An Introduction To Python - Nested Branches, Multiple Alternatives

Hand Tracing The Tax Program

Tax 1 Tax 2 Income MaritalStatus

0 0 8000 m

6400 4000 10400

XXX XXX

Page 9: An Introduction To Python - Nested Branches, Multiple Alternatives

What Is A “Nested Branch”?

• It is often necessary to include an if statement inside another. Such an arrangement is called a nested set of statements.

• Example:

Is the club full?

Arrive at

the club

Are you on the

VIP list?

Wait in car

Go right in

Wait in lineY

Y

N

N

Page 10: An Introduction To Python - Nested Branches, Multiple Alternatives

Example of a “Nested Branch”

if (peopleInClub < maxPeopleInClub) :

if (youName == VIPListName) :

goRightIn

else :

waitInLine

else :

waitInCar

Page 11: An Introduction To Python - Nested Branches, Multiple Alternatives

Multiple Alternatives

• What should you do when you have to make a decision with more than one alternative?

• Example: Telling a student what their grade in a class was…

Page 12: An Introduction To Python - Nested Branches, Multiple Alternatives

Example: Telling Students What Their Grade In A Course Is

If (classScore >=90) :

print(“You got an A!”)

else:

if (classScore >= 80) :

print(“You did ok, you got a B!”)

else:

if (classScore >=70) :

print(“So-so, you got a C”)

else:

if (classScore >= 60) :

print(“Oh –oh, you got a D”)

else :

print(“Dang it, you got an F”)

Image Credit: jghue.blogspot.com

Page 13: An Introduction To Python - Nested Branches, Multiple Alternatives

Problems With “Super Nesting”

• Difficult to read

• Shifted too far to the right due to indentation

Image Credit: www.clipartbest.com

Page 14: An Introduction To Python - Nested Branches, Multiple Alternatives

A Better Way: elif

If (classScore >=90) :

print(“You got an A!”)

elif (classScore >= 80) :

print(“You did ok, you got a B!”)

elif (classScore >=70) :

print(“So-so, you got a C”)

elif (classScore >= 60) :

print(“Oh –oh, you got a D”)

else :

print(“Dang it, you got an F”)

Image Credit: jghue.blogspot.com

Note that you have to test the more specific conditions first.

Page 15: An Introduction To Python - Nested Branches, Multiple Alternatives

Example: Supermarket Coupons

A supermarket awards coupons depending on how much a customer spends on

groceries. For example, if you spend $50, you will get a coupon worth eight percent

of that amount. The following table shows the percent used to calculate the coupon

awarded for different amounts spent. Write a program that calculates and prints the

value of the coupon a person can receive based on groceries purchased.

Page 16: An Introduction To Python - Nested Branches, Multiple Alternatives

What’s In Your Python Toolbox?

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

Page 17: An Introduction To Python - Nested Branches, Multiple Alternatives

What We Covered Today

1. Hand Tracing

2. Nested Branch

3. elif

Image Credit: http://www.tswdj.com/blog/2011/05/17/the-grooms-checklist/

Page 18: An Introduction To Python - Nested Branches, Multiple Alternatives

What We’ll Be Covering Next Time

1. While 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/