SlideShare una empresa de Scribd logo
1 de 18
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/

Más contenido relacionado

Destacado

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 AlgorithmsBlue 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 MaintenanceBlue 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 DesignBlue 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 2Blue 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 DataBlue Elephant Consulting
 
An Introduction To Python - Variables, Math
An Introduction To Python - Variables, MathAn Introduction To Python - Variables, Math
An Introduction To Python - Variables, MathBlue Elephant Consulting
 

Destacado (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
 
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
 
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
 
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
 
An Introduction To Python - Graphics
An Introduction To Python - GraphicsAn Introduction To Python - Graphics
An Introduction To Python - Graphics
 
An Introduction To Python - Dictionaries
An Introduction To Python - DictionariesAn Introduction To Python - Dictionaries
An Introduction To Python - Dictionaries
 
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
 
An Introduction To Python - Variables, Math
An Introduction To Python - Variables, MathAn Introduction To Python - Variables, Math
An Introduction To Python - Variables, Math
 

Similar a An Introduction To Python - Nested Branches, Multiple Alternatives

presentation_python_11_1569171345_375360.pptx
presentation_python_11_1569171345_375360.pptxpresentation_python_11_1569171345_375360.pptx
presentation_python_11_1569171345_375360.pptxGAURAVRATHORE86
 
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 OwnerProduct School
 
Acm aleppo cpc training introduction 1
Acm aleppo cpc training introduction 1Acm aleppo cpc training introduction 1
Acm aleppo cpc training introduction 1Ahmad Bashar Eter
 
Thexfactor 160108194702
Thexfactor 160108194702Thexfactor 160108194702
Thexfactor 160108194702Lori 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 experimentationVWO
 
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 PakistanNikki Wheeler
 
Agile 2010 Estimation Games
Agile 2010 Estimation  GamesAgile 2010 Estimation  Games
Agile 2010 Estimation GamesAgileCoach.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 SuccessBlueHornet
 
Machine learning and_nlp
Machine learning and_nlpMachine learning and_nlp
Machine learning and_nlpankit_ppt
 
Who needs an estimate
Who needs an estimateWho needs an estimate
Who needs an estimateMatt Heusser
 
Understanding computer vision with Deep Learning
Understanding computer vision with Deep LearningUnderstanding computer vision with Deep Learning
Understanding computer vision with Deep LearningCloudxLab
 
Understanding computer vision with Deep Learning
Understanding computer vision with Deep LearningUnderstanding computer vision with Deep Learning
Understanding computer vision with Deep Learningknowbigdata
 
Understanding computer vision with Deep Learning
Understanding computer vision with Deep LearningUnderstanding computer vision with Deep Learning
Understanding computer vision with Deep LearningShubhWadekar
 
Scientific Revenue USF 2016 talk
Scientific Revenue USF 2016 talkScientific Revenue USF 2016 talk
Scientific Revenue USF 2016 talkScientificRevenue
 
How to Create Memorable Data Visualizations
How to Create Memorable Data VisualizationsHow to Create Memorable Data Visualizations
How to Create Memorable Data VisualizationsHanapin Marketing
 

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

presentation_python_11_1569171345_375360.pptx
presentation_python_11_1569171345_375360.pptxpresentation_python_11_1569171345_375360.pptx
presentation_python_11_1569171345_375360.pptx
 
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
 
Acm aleppo cpc training introduction 1
Acm aleppo cpc training introduction 1Acm aleppo cpc training introduction 1
Acm aleppo cpc training introduction 1
 
Thexfactor 160108194702
Thexfactor 160108194702Thexfactor 160108194702
Thexfactor 160108194702
 
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
 
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...
 
Innovation and growth with experimentation
Innovation and growth with experimentationInnovation and growth with experimentation
Innovation and growth with experimentation
 
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)
 
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
 
Agile 2010 Estimation Games
Agile 2010 Estimation  GamesAgile 2010 Estimation  Games
Agile 2010 Estimation Games
 
Marketing analytics
Marketing analyticsMarketing analytics
Marketing analytics
 
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
 
Fast Distributed Online Classification
Fast Distributed Online Classification Fast Distributed Online Classification
Fast Distributed Online Classification
 
Machine learning and_nlp
Machine learning and_nlpMachine learning and_nlp
Machine learning and_nlp
 
Who needs an estimate
Who needs an estimateWho needs an estimate
Who needs an estimate
 
Understanding computer vision with Deep Learning
Understanding computer vision with Deep LearningUnderstanding computer vision with Deep Learning
Understanding computer vision with Deep Learning
 
Understanding computer vision with Deep Learning
Understanding computer vision with Deep LearningUnderstanding computer vision with Deep Learning
Understanding computer vision with Deep Learning
 
Understanding computer vision with Deep Learning
Understanding computer vision with Deep LearningUnderstanding computer vision with Deep Learning
Understanding computer vision with Deep Learning
 
Scientific Revenue USF 2016 talk
Scientific Revenue USF 2016 talkScientific Revenue USF 2016 talk
Scientific Revenue USF 2016 talk
 
How to Create Memorable Data Visualizations
How to Create Memorable Data VisualizationsHow to Create Memorable Data Visualizations
How to Create Memorable Data Visualizations
 

Último

Trauma-Informed Leadership - Five Practical Principles
Trauma-Informed Leadership - Five Practical PrinciplesTrauma-Informed Leadership - Five Practical Principles
Trauma-Informed Leadership - Five Practical PrinciplesPooky Knightsmith
 
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...Nguyen Thanh Tu Collection
 
Basic Civil Engineering notes on Transportation Engineering & Modes of Transport
Basic Civil Engineering notes on Transportation Engineering & Modes of TransportBasic Civil Engineering notes on Transportation Engineering & Modes of Transport
Basic Civil Engineering notes on Transportation Engineering & Modes of TransportDenish Jangid
 
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...Nguyen Thanh Tu Collection
 
e-Sealing at EADTU by Kamakshi Rajagopal
e-Sealing at EADTU by Kamakshi Rajagopale-Sealing at EADTU by Kamakshi Rajagopal
e-Sealing at EADTU by Kamakshi RajagopalEADTU
 
OSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsOSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsSandeep D Chaudhary
 
How to Send Pro Forma Invoice to Your Customers in Odoo 17
How to Send Pro Forma Invoice to Your Customers in Odoo 17How to Send Pro Forma Invoice to Your Customers in Odoo 17
How to Send Pro Forma Invoice to Your Customers in Odoo 17Celine George
 
When Quality Assurance Meets Innovation in Higher Education - Report launch w...
When Quality Assurance Meets Innovation in Higher Education - Report launch w...When Quality Assurance Meets Innovation in Higher Education - Report launch w...
When Quality Assurance Meets Innovation in Higher Education - Report launch w...Gary Wood
 
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文中 央社
 
The Liver & Gallbladder (Anatomy & Physiology).pptx
The Liver &  Gallbladder (Anatomy & Physiology).pptxThe Liver &  Gallbladder (Anatomy & Physiology).pptx
The Liver & Gallbladder (Anatomy & Physiology).pptxVishal Singh
 
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading RoomSternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading RoomSean M. Fox
 
ANTI PARKISON DRUGS.pptx
ANTI         PARKISON          DRUGS.pptxANTI         PARKISON          DRUGS.pptx
ANTI PARKISON DRUGS.pptxPoojaSen20
 
MOOD STABLIZERS DRUGS.pptx
MOOD     STABLIZERS           DRUGS.pptxMOOD     STABLIZERS           DRUGS.pptx
MOOD STABLIZERS DRUGS.pptxPoojaSen20
 
Graduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptxGraduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptxneillewis46
 
UChicago CMSC 23320 - The Best Commit Messages of 2024
UChicago CMSC 23320 - The Best Commit Messages of 2024UChicago CMSC 23320 - The Best Commit Messages of 2024
UChicago CMSC 23320 - The Best Commit Messages of 2024Borja Sotomayor
 
PSYPACT- Practicing Over State Lines May 2024.pptx
PSYPACT- Practicing Over State Lines May 2024.pptxPSYPACT- Practicing Over State Lines May 2024.pptx
PSYPACT- Practicing Over State Lines May 2024.pptxMarlene Maheu
 
8 Tips for Effective Working Capital Management
8 Tips for Effective Working Capital Management8 Tips for Effective Working Capital Management
8 Tips for Effective Working Capital ManagementMBA Assignment Experts
 
Major project report on Tata Motors and its marketing strategies
Major project report on Tata Motors and its marketing strategiesMajor project report on Tata Motors and its marketing strategies
Major project report on Tata Motors and its marketing strategiesAmanpreetKaur157993
 

Último (20)

Trauma-Informed Leadership - Five Practical Principles
Trauma-Informed Leadership - Five Practical PrinciplesTrauma-Informed Leadership - Five Practical Principles
Trauma-Informed Leadership - Five Practical Principles
 
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
 
Mattingly "AI and Prompt Design: LLMs with NER"
Mattingly "AI and Prompt Design: LLMs with NER"Mattingly "AI and Prompt Design: LLMs with NER"
Mattingly "AI and Prompt Design: LLMs with NER"
 
Basic Civil Engineering notes on Transportation Engineering & Modes of Transport
Basic Civil Engineering notes on Transportation Engineering & Modes of TransportBasic Civil Engineering notes on Transportation Engineering & Modes of Transport
Basic Civil Engineering notes on Transportation Engineering & Modes of Transport
 
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
 
e-Sealing at EADTU by Kamakshi Rajagopal
e-Sealing at EADTU by Kamakshi Rajagopale-Sealing at EADTU by Kamakshi Rajagopal
e-Sealing at EADTU by Kamakshi Rajagopal
 
OSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsOSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & Systems
 
How to Send Pro Forma Invoice to Your Customers in Odoo 17
How to Send Pro Forma Invoice to Your Customers in Odoo 17How to Send Pro Forma Invoice to Your Customers in Odoo 17
How to Send Pro Forma Invoice to Your Customers in Odoo 17
 
When Quality Assurance Meets Innovation in Higher Education - Report launch w...
When Quality Assurance Meets Innovation in Higher Education - Report launch w...When Quality Assurance Meets Innovation in Higher Education - Report launch w...
When Quality Assurance Meets Innovation in Higher Education - Report launch w...
 
OS-operating systems- ch05 (CPU Scheduling) ...
OS-operating systems- ch05 (CPU Scheduling) ...OS-operating systems- ch05 (CPU Scheduling) ...
OS-operating systems- ch05 (CPU Scheduling) ...
 
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
 
The Liver & Gallbladder (Anatomy & Physiology).pptx
The Liver &  Gallbladder (Anatomy & Physiology).pptxThe Liver &  Gallbladder (Anatomy & Physiology).pptx
The Liver & Gallbladder (Anatomy & Physiology).pptx
 
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading RoomSternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
 
ANTI PARKISON DRUGS.pptx
ANTI         PARKISON          DRUGS.pptxANTI         PARKISON          DRUGS.pptx
ANTI PARKISON DRUGS.pptx
 
MOOD STABLIZERS DRUGS.pptx
MOOD     STABLIZERS           DRUGS.pptxMOOD     STABLIZERS           DRUGS.pptx
MOOD STABLIZERS DRUGS.pptx
 
Graduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptxGraduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptx
 
UChicago CMSC 23320 - The Best Commit Messages of 2024
UChicago CMSC 23320 - The Best Commit Messages of 2024UChicago CMSC 23320 - The Best Commit Messages of 2024
UChicago CMSC 23320 - The Best Commit Messages of 2024
 
PSYPACT- Practicing Over State Lines May 2024.pptx
PSYPACT- Practicing Over State Lines May 2024.pptxPSYPACT- Practicing Over State Lines May 2024.pptx
PSYPACT- Practicing Over State Lines May 2024.pptx
 
8 Tips for Effective Working Capital Management
8 Tips for Effective Working Capital Management8 Tips for Effective Working Capital Management
8 Tips for Effective Working Capital Management
 
Major project report on Tata Motors and its marketing strategies
Major project report on Tata Motors and its marketing strategiesMajor project report on Tata Motors and its marketing strategies
Major project report on Tata Motors and its marketing strategies
 

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/

Notas del editor

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