P_29 Multiple if Statements in Python  | Python Tutorials for Beginners

P_29 Multiple if Statements in Python | Python Tutorials for Beginners

Control Statements in Python: Multiple If Statements

Overview of Previous Concepts

  • Discussed various control statements in Python, including simple if statements, if-else structures, nested ifs, and elif ladders.
  • Reviewed a coding exercise on determining leap years as a practical application of these concepts.

Introduction to Multiple If Statements

  • The focus of this video is on implementing multiple if statements for more complex decision-making scenarios.
  • An example involving height and age restrictions for riding a roller coaster was introduced to illustrate the need for multiple conditions.

Example Scenario: Age-Based Ticket Pricing

  • A ticket pricing structure based on age was outlined:
  • Under 12 years: ₹150
  • Between 12 and 18 years: ₹250
  • Over 18 years: ₹500

This structure emphasizes the importance of checking conditions efficiently.

Efficiency in Condition Checking

  • Highlighted that using multiple if statements allows only one condition to be executed rather than checking all conditions sequentially, which can lead to inefficiency.
  • Explained that with an else-if (elif) structure, once a true condition is found, subsequent conditions are not checked, improving efficiency.

When to Use Multiple If Statements

  • Sometimes it’s necessary to check multiple independent conditions; this is where multiple if statements come into play. Each true condition will execute its corresponding block of code without skipping others.
  • General syntax for writing multiple if statements was provided:
  • if condition1 then do X
  • if condition2 then do Y
  • if condition3 then do Z

This ensures all relevant actions are taken when their respective conditions are met.

Practical Application with Additional Conditions

  • Introduced an additional scenario where taking photos incurs an extra charge regardless of the ticket price:
  • Extra charge for photos: ₹50.

This adds complexity but demonstrates how to integrate additional checks within existing logic effectively.

Logic Flow and Implementation Strategy

  • Suggested asking users about photo preferences after determining their age-based ticket price:
  • For each age category (under 12, between 12 and 18, over 18), inquire whether they want photos.

This approach avoids redundancy by consolidating similar checks into one logical flow rather than repeating them across different age categories.

By structuring the notes this way with clear timestamps linked directly to specific insights from the transcript, readers can easily navigate through key concepts discussed in the video while reinforcing their understanding of control statements in Python programming.

Ticket Pricing and Photo Options Explained

Age-Based Ticket Pricing

  • The ticket pricing is categorized based on age:
  • Under 12 years: ₹150
  • Between 12 to 18 years: ₹250
  • Above 18 years: ₹500

Additional Photo Option

  • An additional condition regarding photo options has been introduced:
  • Customers will be asked if they want photos, applicable to all age categories.

Extra Charges for Photos

  • If the customer opts for photos, an extra charge of ₹50 will be added to the ticket price.
  • If the customer declines, the total bill will reflect only the base ticket price.

Program Logic Implementation

  • The program should prompt users about their desire for photos after determining their ticket category.
  • A variable want_photo will capture user input (yes or no), which influences the final bill calculation.

Conditional Logic in Code

  • The program checks if want_photo equals 'y' or 'Y' to determine if an additional charge applies.
  • A variable bill starts at zero and is updated based on age categories before adding any photo charges.

Final Bill Calculation and Output

  • After calculating potential charges, the program outputs the total amount due based on conditions met.
  • Proper indentation in code is crucial; it affects how conditions are evaluated and what output is generated.

Example Scenario Execution

  • For example, if a user inputs their age as 9, they would see a ticket price of ₹150.
  • If they then choose to take a photo (answering yes), their total bill becomes ₹200 (₹150 + ₹50).

Conclusion and User Experience Enhancement

  • Finally, after displaying the total bill, a friendly message like "Thank you! Enjoy your ride!" can enhance user experience.
Video description

In this lecture we have discussed multiple if statements with real life example and flowchart. We have written down a program to show working of "multiple if" in python. Best Python Tutorials for Beginners: https://www.youtube.com/playlist?list=PLdo5W4Nhv31bZSiqiOL5ta39vSnBxpOPT Notes of Today's Class: https://drive.google.com/file/d/1COwYAxLLa_xpMLsU4jjcTOPOvdzSSAAD/view?usp=sharing ********************************************* Connect & Contact Me: My Second Channel Link: https://bit.ly/354n7C7 Facebook: https://www.facebook.com/Jennys-Lectures-CSIT-Netjrf-316814368950701/ Quora: https://www.quora.com/profile/Jayanti-Khatri-Lamba Instagram: https://www.instagram.com/jayantikhatrilamba/ Twitter: https://twitter.com/KhatriJenny ******************************************* More Playlists: Programming in C Tutorials: https://www.youtube.com/playlist?list=PLdo5W4Nhv31a8UcMN9-35ghv8qyFWD9_S C++ Tutorials for beginners: https://www.youtube.com/watch?v=KYxLEDF6kjs&list=PLdo5W4Nhv31YU5Wx1dopka58teWP9aCee Placement Series: https://www.youtube.com/playlist?list=PLdo5W4Nhv31YvlDpJhvOYbM9Ap8UypgEy Data Structures and Algorithms: https: https://www.youtube.com/playlist?list=PLdo5W4Nhv31bbKJzrsKfMpo_grxuLl8LU Design and Analysis of Algorithms(DAA): https://www.youtube.com/playlist?list=PLdo5W4Nhv31ZTn2P9vF02bkb3SC8uiUUn Dynamic Programming: https://www.youtube.com/playlist?list=PLdo5W4Nhv31aBrJE1WS4MR9LRfbmZrAQu Operating Systems tutorials: //www.youtube.com/playlist?list=PLdo5W4Nhv31a5ucW_S1K3-x6ztBRD-PNa DBMS Tutorials: https://www.youtube.com/playlist?list=PLdo5W4Nhv31b33kF46f9aFjoJPOkdlsRc #coding #pythonforbeginners #python #jennyslectures #pythonprogramming