SlideShare a Scribd company logo
An Introduction To Software
Development Using Python
Spring Semester, 2015
Class #6:
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
“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
Example: Let’s Talk About Taxes
Federal Tax Rate Schedule
Image Credit: www.clipartpanda.com
Hand Tracing The Tax Program
Tax 1 Tax 2 Income Marital
Status
80000 m
Hand Tracing The Tax Program
Tax 1 Tax 2 Income Marital
Status
0 0 8000 m
Hand Tracing The Tax Program
Tax 1 Tax 2 Income Marital
Status
0 0 8000 m
6400 4000
XXX XXX
Hand Tracing The Tax Program
Tax 1 Tax 2 Income Marital
Status
0 0 8000 m
6400 4000 10400
XXX XXX
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 line
Y
Y
N
N
Example of a “Nested Branch”
if (peopleInClub < maxPeopleInClub) :
if (youName == VIPListName) :
goRightIn
else :
waitInLine
else :
waitInCar
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…
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
Problems With “Super Nesting”
• Difficult to read
• Shifted too far to the right due to indentation
Image Credit: www.clipartbest.com
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.
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.
What’s In Your Python Toolbox?
print() math strings I/O IF/Else elif
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/
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/
Ad

More Related Content

Viewers also liked (8)

An Introduction To Python - Tables, List Algorithms
An Introduction To Python - Tables, List AlgorithmsAn Introduction To Python - Tables, List Algorithms
An Introduction To Python - Tables, List Algorithms
Blue Elephant Consulting
 
An Introduction To Software Development - Software Support and Maintenance
An Introduction To Software Development - Software Support and MaintenanceAn Introduction To Software Development - Software Support and Maintenance
An Introduction To Software Development - Software Support and Maintenance
Blue Elephant Consulting
 
An Introduction To Software Development - Architecture & Detailed Design
An Introduction To Software Development - Architecture & Detailed DesignAn Introduction To Software Development - Architecture & Detailed Design
An Introduction To Software Development - Architecture & Detailed Design
Blue Elephant Consulting
 
An Introduction To Python - Functions, Part 2
An Introduction To Python - Functions, Part 2An Introduction To Python - Functions, Part 2
An Introduction To Python - Functions, Part 2
Blue Elephant Consulting
 
An Introduction To Python - Graphics
An Introduction To Python - GraphicsAn Introduction To Python - Graphics
An Introduction To Python - Graphics
Blue Elephant Consulting
 
An Introduction To Python - Dictionaries
An Introduction To Python - DictionariesAn Introduction To Python - Dictionaries
An Introduction To Python - Dictionaries
Blue Elephant Consulting
 
An Introduction To Python - Working With Data
An Introduction To Python - Working With DataAn Introduction To Python - Working With Data
An Introduction To Python - Working With Data
Blue Elephant Consulting
 
An Introduction To Python - Variables, Math
An Introduction To Python - Variables, MathAn Introduction To Python - Variables, Math
An Introduction To Python - Variables, Math
Blue Elephant Consulting
 
An Introduction To Python - Tables, List Algorithms
An Introduction To Python - Tables, List AlgorithmsAn Introduction To Python - Tables, List Algorithms
An Introduction To Python - Tables, List Algorithms
Blue Elephant Consulting
 
An Introduction To Software Development - Software Support and Maintenance
An Introduction To Software Development - Software Support and MaintenanceAn Introduction To Software Development - Software Support and Maintenance
An Introduction To Software Development - Software Support and Maintenance
Blue Elephant Consulting
 
An Introduction To Software Development - Architecture & Detailed Design
An Introduction To Software Development - Architecture & Detailed DesignAn Introduction To Software Development - Architecture & Detailed Design
An Introduction To Software Development - Architecture & Detailed Design
Blue Elephant Consulting
 
An Introduction To Python - Functions, Part 2
An Introduction To Python - Functions, Part 2An Introduction To Python - Functions, Part 2
An Introduction To Python - Functions, Part 2
Blue Elephant Consulting
 
An Introduction To Python - Working With Data
An Introduction To Python - Working With DataAn Introduction To Python - Working With Data
An Introduction To Python - Working With Data
Blue Elephant Consulting
 
An Introduction To Python - Variables, Math
An Introduction To Python - Variables, MathAn Introduction To Python - Variables, Math
An Introduction To Python - Variables, Math
Blue Elephant Consulting
 

Similar to An Introduction To Python - Nested Branches, Multiple Alternatives (20)

How to Measure Important Data by fmr Booking.com Product Owner
How to Measure Important Data by fmr Booking.com Product OwnerHow to Measure Important Data by fmr Booking.com Product Owner
How to Measure Important Data by fmr Booking.com Product Owner
Product School
 
Acm aleppo cpc training introduction 1
Acm aleppo cpc training introduction 1Acm aleppo cpc training introduction 1
Acm aleppo cpc training introduction 1
Ahmad Bashar Eter
 
Thexfactor 160108194702
Thexfactor 160108194702Thexfactor 160108194702
Thexfactor 160108194702
Lori Trafford
 
The X factor: The Secret to Better Content Marketing
The X factor: The Secret to Better Content Marketing The X factor: The Secret to Better Content Marketing
The X factor: The Secret to Better Content Marketing
Mathew Sweezey
 
Support Vector Machine - How Support Vector Machine works | SVM in Machine Le...
Support Vector Machine - How Support Vector Machine works | SVM in Machine Le...Support Vector Machine - How Support Vector Machine works | SVM in Machine Le...
Support Vector Machine - How Support Vector Machine works | SVM in Machine Le...
Simplilearn
 
Innovation and growth with experimentation
Innovation and growth with experimentationInnovation and growth with experimentation
Innovation and growth with experimentation
VWO
 
Transferring Software Testing Tools to Practice (AST 2017 Keynote)
Transferring Software Testing Tools to Practice (AST 2017 Keynote)Transferring Software Testing Tools to Practice (AST 2017 Keynote)
Transferring Software Testing Tools to Practice (AST 2017 Keynote)
Tao Xie
 
Essay On My Role Against Corruption In Pakistan
Essay On My Role Against Corruption In PakistanEssay On My Role Against Corruption In Pakistan
Essay On My Role Against Corruption In Pakistan
Nikki Wheeler
 
Agile 2010 Estimation Games
Agile 2010 Estimation  GamesAgile 2010 Estimation  Games
Agile 2010 Estimation Games
AgileCoach.net
 
Marketing analytics
Marketing analyticsMarketing analytics
Marketing analytics
Data Science Thailand
 
LCMC Session: Email and Web Conversion Success
LCMC Session: Email and Web Conversion SuccessLCMC Session: Email and Web Conversion Success
LCMC Session: Email and Web Conversion Success
BlueHornet
 
Fast Distributed Online Classification
Fast Distributed Online Classification Fast Distributed Online Classification
Fast Distributed Online Classification
DataWorks Summit/Hadoop Summit
 
Machine learning and_nlp
Machine learning and_nlpMachine learning and_nlp
Machine learning and_nlp
ankit_ppt
 
Who needs an estimate
Who needs an estimateWho needs an estimate
Who needs an estimate
Matt Heusser
 
Understanding computer vision with Deep Learning
Understanding computer vision with Deep LearningUnderstanding computer vision with Deep Learning
Understanding computer vision with Deep Learning
ShubhWadekar
 
Understanding computer vision with Deep Learning
Understanding computer vision with Deep LearningUnderstanding computer vision with Deep Learning
Understanding computer vision with Deep Learning
CloudxLab
 
Understanding computer vision with Deep Learning
Understanding computer vision with Deep LearningUnderstanding computer vision with Deep Learning
Understanding computer vision with Deep Learning
knowbigdata
 
Scientific Revenue USF 2016 talk
Scientific Revenue USF 2016 talkScientific Revenue USF 2016 talk
Scientific Revenue USF 2016 talk
ScientificRevenue
 
How to Create Memorable Data Visualizations
How to Create Memorable Data VisualizationsHow to Create Memorable Data Visualizations
How to Create Memorable Data Visualizations
Hanapin Marketing
 
Scalable Learning Technologies for Big Data Mining
Scalable Learning Technologies for Big Data MiningScalable Learning Technologies for Big Data Mining
Scalable Learning Technologies for Big Data Mining
Gerard de Melo
 
How to Measure Important Data by fmr Booking.com Product Owner
How to Measure Important Data by fmr Booking.com Product OwnerHow to Measure Important Data by fmr Booking.com Product Owner
How to Measure Important Data by fmr Booking.com Product Owner
Product School
 
Acm aleppo cpc training introduction 1
Acm aleppo cpc training introduction 1Acm aleppo cpc training introduction 1
Acm aleppo cpc training introduction 1
Ahmad Bashar Eter
 
Thexfactor 160108194702
Thexfactor 160108194702Thexfactor 160108194702
Thexfactor 160108194702
Lori Trafford
 
The X factor: The Secret to Better Content Marketing
The X factor: The Secret to Better Content Marketing The X factor: The Secret to Better Content Marketing
The X factor: The Secret to Better Content Marketing
Mathew Sweezey
 
Support Vector Machine - How Support Vector Machine works | SVM in Machine Le...
Support Vector Machine - How Support Vector Machine works | SVM in Machine Le...Support Vector Machine - How Support Vector Machine works | SVM in Machine Le...
Support Vector Machine - How Support Vector Machine works | SVM in Machine Le...
Simplilearn
 
Innovation and growth with experimentation
Innovation and growth with experimentationInnovation and growth with experimentation
Innovation and growth with experimentation
VWO
 
Transferring Software Testing Tools to Practice (AST 2017 Keynote)
Transferring Software Testing Tools to Practice (AST 2017 Keynote)Transferring Software Testing Tools to Practice (AST 2017 Keynote)
Transferring Software Testing Tools to Practice (AST 2017 Keynote)
Tao Xie
 
Essay On My Role Against Corruption In Pakistan
Essay On My Role Against Corruption In PakistanEssay On My Role Against Corruption In Pakistan
Essay On My Role Against Corruption In Pakistan
Nikki Wheeler
 
Agile 2010 Estimation Games
Agile 2010 Estimation  GamesAgile 2010 Estimation  Games
Agile 2010 Estimation Games
AgileCoach.net
 
LCMC Session: Email and Web Conversion Success
LCMC Session: Email and Web Conversion SuccessLCMC Session: Email and Web Conversion Success
LCMC Session: Email and Web Conversion Success
BlueHornet
 
Machine learning and_nlp
Machine learning and_nlpMachine learning and_nlp
Machine learning and_nlp
ankit_ppt
 
Who needs an estimate
Who needs an estimateWho needs an estimate
Who needs an estimate
Matt Heusser
 
Understanding computer vision with Deep Learning
Understanding computer vision with Deep LearningUnderstanding computer vision with Deep Learning
Understanding computer vision with Deep Learning
ShubhWadekar
 
Understanding computer vision with Deep Learning
Understanding computer vision with Deep LearningUnderstanding computer vision with Deep Learning
Understanding computer vision with Deep Learning
CloudxLab
 
Understanding computer vision with Deep Learning
Understanding computer vision with Deep LearningUnderstanding computer vision with Deep Learning
Understanding computer vision with Deep Learning
knowbigdata
 
Scientific Revenue USF 2016 talk
Scientific Revenue USF 2016 talkScientific Revenue USF 2016 talk
Scientific Revenue USF 2016 talk
ScientificRevenue
 
How to Create Memorable Data Visualizations
How to Create Memorable Data VisualizationsHow to Create Memorable Data Visualizations
How to Create Memorable Data Visualizations
Hanapin Marketing
 
Scalable Learning Technologies for Big Data Mining
Scalable Learning Technologies for Big Data MiningScalable Learning Technologies for Big Data Mining
Scalable Learning Technologies for Big Data Mining
Gerard de Melo
 
Ad

Recently uploaded (20)

SCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptx
SCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptxSCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptx
SCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptx
Ronisha Das
 
Understanding P–N Junction Semiconductors: A Beginner’s Guide
Understanding P–N Junction Semiconductors: A Beginner’s GuideUnderstanding P–N Junction Semiconductors: A Beginner’s Guide
Understanding P–N Junction Semiconductors: A Beginner’s Guide
GS Virdi
 
2541William_McCollough_DigitalDetox.docx
2541William_McCollough_DigitalDetox.docx2541William_McCollough_DigitalDetox.docx
2541William_McCollough_DigitalDetox.docx
contactwilliamm2546
 
Sinhala_Male_Names.pdf Sinhala_Male_Name
Sinhala_Male_Names.pdf Sinhala_Male_NameSinhala_Male_Names.pdf Sinhala_Male_Name
Sinhala_Male_Names.pdf Sinhala_Male_Name
keshanf79
 
YSPH VMOC Special Report - Measles Outbreak Southwest US 4-30-2025.pptx
YSPH VMOC Special Report - Measles Outbreak  Southwest US 4-30-2025.pptxYSPH VMOC Special Report - Measles Outbreak  Southwest US 4-30-2025.pptx
YSPH VMOC Special Report - Measles Outbreak Southwest US 4-30-2025.pptx
Yale School of Public Health - The Virtual Medical Operations Center (VMOC)
 
Anti-Depressants pharmacology 1slide.pptx
Anti-Depressants pharmacology 1slide.pptxAnti-Depressants pharmacology 1slide.pptx
Anti-Depressants pharmacology 1slide.pptx
Mayuri Chavan
 
UNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACY
UNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACYUNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACY
UNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACY
DR.PRISCILLA MARY J
 
Metamorphosis: Life's Transformative Journey
Metamorphosis: Life's Transformative JourneyMetamorphosis: Life's Transformative Journey
Metamorphosis: Life's Transformative Journey
Arshad Shaikh
 
Ultimate VMware 2V0-11.25 Exam Dumps for Exam Success
Ultimate VMware 2V0-11.25 Exam Dumps for Exam SuccessUltimate VMware 2V0-11.25 Exam Dumps for Exam Success
Ultimate VMware 2V0-11.25 Exam Dumps for Exam Success
Mark Soia
 
Odoo Inventory Rules and Routes v17 - Odoo Slides
Odoo Inventory Rules and Routes v17 - Odoo SlidesOdoo Inventory Rules and Routes v17 - Odoo Slides
Odoo Inventory Rules and Routes v17 - Odoo Slides
Celine George
 
World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...
World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...
World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...
larencebapu132
 
How to Manage Opening & Closing Controls in Odoo 17 POS
How to Manage Opening & Closing Controls in Odoo 17 POSHow to Manage Opening & Closing Controls in Odoo 17 POS
How to Manage Opening & Closing Controls in Odoo 17 POS
Celine George
 
Operations Management (Dr. Abdulfatah Salem).pdf
Operations Management (Dr. Abdulfatah Salem).pdfOperations Management (Dr. Abdulfatah Salem).pdf
Operations Management (Dr. Abdulfatah Salem).pdf
Arab Academy for Science, Technology and Maritime Transport
 
LDMMIA Reiki Master Spring 2025 Mini Updates
LDMMIA Reiki Master Spring 2025 Mini UpdatesLDMMIA Reiki Master Spring 2025 Mini Updates
LDMMIA Reiki Master Spring 2025 Mini Updates
LDM Mia eStudios
 
Michelle Rumley & Mairéad Mooney, Boole Library, University College Cork. Tra...
Michelle Rumley & Mairéad Mooney, Boole Library, University College Cork. Tra...Michelle Rumley & Mairéad Mooney, Boole Library, University College Cork. Tra...
Michelle Rumley & Mairéad Mooney, Boole Library, University College Cork. Tra...
Library Association of Ireland
 
P-glycoprotein pamphlet: iteration 4 of 4 final
P-glycoprotein pamphlet: iteration 4 of 4 finalP-glycoprotein pamphlet: iteration 4 of 4 final
P-glycoprotein pamphlet: iteration 4 of 4 final
bs22n2s
 
Unit 6_Introduction_Phishing_Password Cracking.pdf
Unit 6_Introduction_Phishing_Password Cracking.pdfUnit 6_Introduction_Phishing_Password Cracking.pdf
Unit 6_Introduction_Phishing_Password Cracking.pdf
KanchanPatil34
 
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - WorksheetCBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
Sritoma Majumder
 
How to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
How to Customize Your Financial Reports & Tax Reports With Odoo 17 AccountingHow to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
How to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
Celine George
 
YSPH VMOC Special Report - Measles Outbreak Southwest US 5-3-2025.pptx
YSPH VMOC Special Report - Measles Outbreak  Southwest US 5-3-2025.pptxYSPH VMOC Special Report - Measles Outbreak  Southwest US 5-3-2025.pptx
YSPH VMOC Special Report - Measles Outbreak Southwest US 5-3-2025.pptx
Yale School of Public Health - The Virtual Medical Operations Center (VMOC)
 
SCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptx
SCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptxSCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptx
SCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptx
Ronisha Das
 
Understanding P–N Junction Semiconductors: A Beginner’s Guide
Understanding P–N Junction Semiconductors: A Beginner’s GuideUnderstanding P–N Junction Semiconductors: A Beginner’s Guide
Understanding P–N Junction Semiconductors: A Beginner’s Guide
GS Virdi
 
2541William_McCollough_DigitalDetox.docx
2541William_McCollough_DigitalDetox.docx2541William_McCollough_DigitalDetox.docx
2541William_McCollough_DigitalDetox.docx
contactwilliamm2546
 
Sinhala_Male_Names.pdf Sinhala_Male_Name
Sinhala_Male_Names.pdf Sinhala_Male_NameSinhala_Male_Names.pdf Sinhala_Male_Name
Sinhala_Male_Names.pdf Sinhala_Male_Name
keshanf79
 
Anti-Depressants pharmacology 1slide.pptx
Anti-Depressants pharmacology 1slide.pptxAnti-Depressants pharmacology 1slide.pptx
Anti-Depressants pharmacology 1slide.pptx
Mayuri Chavan
 
UNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACY
UNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACYUNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACY
UNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACY
DR.PRISCILLA MARY J
 
Metamorphosis: Life's Transformative Journey
Metamorphosis: Life's Transformative JourneyMetamorphosis: Life's Transformative Journey
Metamorphosis: Life's Transformative Journey
Arshad Shaikh
 
Ultimate VMware 2V0-11.25 Exam Dumps for Exam Success
Ultimate VMware 2V0-11.25 Exam Dumps for Exam SuccessUltimate VMware 2V0-11.25 Exam Dumps for Exam Success
Ultimate VMware 2V0-11.25 Exam Dumps for Exam Success
Mark Soia
 
Odoo Inventory Rules and Routes v17 - Odoo Slides
Odoo Inventory Rules and Routes v17 - Odoo SlidesOdoo Inventory Rules and Routes v17 - Odoo Slides
Odoo Inventory Rules and Routes v17 - Odoo Slides
Celine George
 
World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...
World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...
World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...
larencebapu132
 
How to Manage Opening & Closing Controls in Odoo 17 POS
How to Manage Opening & Closing Controls in Odoo 17 POSHow to Manage Opening & Closing Controls in Odoo 17 POS
How to Manage Opening & Closing Controls in Odoo 17 POS
Celine George
 
LDMMIA Reiki Master Spring 2025 Mini Updates
LDMMIA Reiki Master Spring 2025 Mini UpdatesLDMMIA Reiki Master Spring 2025 Mini Updates
LDMMIA Reiki Master Spring 2025 Mini Updates
LDM Mia eStudios
 
Michelle Rumley & Mairéad Mooney, Boole Library, University College Cork. Tra...
Michelle Rumley & Mairéad Mooney, Boole Library, University College Cork. Tra...Michelle Rumley & Mairéad Mooney, Boole Library, University College Cork. Tra...
Michelle Rumley & Mairéad Mooney, Boole Library, University College Cork. Tra...
Library Association of Ireland
 
P-glycoprotein pamphlet: iteration 4 of 4 final
P-glycoprotein pamphlet: iteration 4 of 4 finalP-glycoprotein pamphlet: iteration 4 of 4 final
P-glycoprotein pamphlet: iteration 4 of 4 final
bs22n2s
 
Unit 6_Introduction_Phishing_Password Cracking.pdf
Unit 6_Introduction_Phishing_Password Cracking.pdfUnit 6_Introduction_Phishing_Password Cracking.pdf
Unit 6_Introduction_Phishing_Password Cracking.pdf
KanchanPatil34
 
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - WorksheetCBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
Sritoma Majumder
 
How to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
How to Customize Your Financial Reports & Tax Reports With Odoo 17 AccountingHow to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
How to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
Celine George
 
Ad

An Introduction To Python - Nested Branches, Multiple Alternatives

  • 1. An Introduction To Software Development Using Python Spring Semester, 2015 Class #6: Nested Branches, Multiple Alternatives
  • 2. 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
  • 3. “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
  • 4. Example: Let’s Talk About Taxes Federal Tax Rate Schedule Image Credit: www.clipartpanda.com
  • 5. Hand Tracing The Tax Program Tax 1 Tax 2 Income Marital Status 80000 m
  • 6. Hand Tracing The Tax Program Tax 1 Tax 2 Income Marital Status 0 0 8000 m
  • 7. Hand Tracing The Tax Program Tax 1 Tax 2 Income Marital Status 0 0 8000 m 6400 4000 XXX XXX
  • 8. Hand Tracing The Tax Program Tax 1 Tax 2 Income Marital Status 0 0 8000 m 6400 4000 10400 XXX XXX
  • 9. 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 line Y Y N N
  • 10. Example of a “Nested Branch” if (peopleInClub < maxPeopleInClub) : if (youName == VIPListName) : goRightIn else : waitInLine else : waitInCar
  • 11. 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…
  • 12. 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
  • 13. Problems With “Super Nesting” • Difficult to read • Shifted too far to the right due to indentation Image Credit: www.clipartbest.com
  • 14. 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.
  • 15. 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.
  • 16. What’s In Your Python Toolbox? print() math strings I/O IF/Else elif
  • 17. 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/
  • 18. 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/

Editor's Notes

  • #2: 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.