• Decision Making in Python Part 2

    Decision Making in Python Part 2 

    Using the if...elif statement
    You go to a restaurant and look at the menu. The restaurant offers eggs, chicken, mutton, and fish. After you choose one of the items, the server brings it to you. Creating a menu selection requires something like an if...else statement, but with a little extra effort. In this case, you use the elif clause to create another set of conditions. The elif clause is a combination of the else clause and a separate if statement. The following steps describe how to use the if...elif statement to create a menu.
    1. Open a Python File window.
    You see an editor in which you can type the example code.
    2. Type the following code into the window — pressing Enter after each line:
    print("1. Jan")
    print("2. feb")
    print("3. March")
    print("4. April")
    print("5. May")
    print("6. June")
    Choice = int(input("Select your favorite Month: "))
    if (Choice == 1):
    print("You chose Jan!")
    elif (Choice == 2):
    print("You chose feb!")
    elif (Choice == 3):
    print("You chose March!")
    elif (Choice == 4):
    print("You chose April!")
    elif (Choice == 5):
    print("You chose May!")
    elif (Choice == 6):
    print("You chose June!")
    else:
    print("You made an invalid choice!")

    The example begins by displaying a menu. The user sees a list of choices for the application. It then asks the user to make a selection, which it places inside Choice. The use of the int() function ensures that the user can’t type anything other than a number.


    Using Nested Statements
    The decision-making process often happens in levels. For example, when you go to the restaurant and choose eggs for breakfast, you have made a first-level decision. Now the server asks you what type of toast you want with your eggs. The server wouldn’t ask this question if you had ordered pancakes, so the selection of toast becomes a second-level decision. When the breakfast arrives, you decide whether you want to use jelly on your toast. This is a third-level decision. If you had selected a kind of toast that doesn’t work well with jelly, you might not have had to make this decision at all. This process of making decisions in levels, with each level reliant on the decision made at the previous level, is called nesting

    Using multiple if or if...else statements
    The most commonly used multiple selection technique is a combination of if and if...else statements. This form of selection is often called a selection tree because of its resemblance to the branches of a tree. In this case, you follow a particular path to obtain a desired result.

    1. Open a Python File window.
    You see an editor where you can type the example code.
    2. Type the following code into the window — pressing Enter after each
    line:
    One = int(input("Type a number between 1 and 10: "))
    Two = int(input("Type a number between 1 and 10: "))
    if (One >= 1) and (One <= 10):
    if (Two >= 1) and (Two <= 10):
    print("Your secret number is: ", One * Two)
    else:
    print("Incorrect second value!")
    else:
    print("Incorrect first value!")

    The second if...else statement is indented within the first if...else statement. The indentation tells Python that this is a second-level statement.

    3. Choose RunRun Module.
    You see a Python Shell window open with a prompt to type a number between 1 and 10.

    4. Type and press Enter.
    The shell asks for another number between 1 and 10.

    5. Type and press Enter.
    You see the combination of the two numbers

    For example, if you attempt to provide a value that’s outside the requested range, you see an error message. The error message is tailored for either the first or second input value so that the user knows which value was incorrect. Providing specific error messages is always useful because users tend to become confused and frustrated otherwise. In addition, a specific error message
    helps you find errors in your application much faster.

    It’s possible to use any combination of if, if...else, and if...elif statements to produce a desired outcome. You can nest the code blocks as many levels deep as needed to perform the required checks 

    Listing 7-1: Creating a Breakfast Menu
    print("1. Eggs")
    print("2. Pancakes")
    print("3. Waffles")
    print("4. Oatmeal")
    MainChoice = int(input("Choose a breakfast item: "))
    if (MainChoice == 2):
    Meal = "Pancakes"
    elif (MainChoice == 3):
    Meal = "Waffles"
    if (MainChoice == 1):
    print("1. Wheat Toast")
    print("2. Sour Dough")
    print("3. Rye Toast")
    print("4. Pancakes")
    Bread = int(input("Choose a type of bread: "))
    if (Bread == 1):
    print("You chose eggs with wheat toast.")
    elif (Bread == 2):
    print("You chose eggs with sour dough.")
    elif (Bread == 3):
    print("You chose eggs with rye toast.")
    elif (Bread == 4):
    print("You chose eggs with pancakes.")
    else:
    print("We have eggs, but not that kind of bread.")
    elif (MainChoice == 2) or (MainChoice == 3):
    print("1. Syrup")
    print("2. Strawberries")
    print("3. Powdered Sugar")
    Topping = int(input("Choose a topping: "))
    if (Topping == 1):
    print ("You chose " + Meal + " with syrup.")
    elif (Topping == 2):
    print ("You chose " + Meal + " with strawberries.")
    elif (Topping == 3):
    print ("You chose " + Meal + " with powdered sugar.")
    else:
    print ("We have " + Meal + ", but not that topping.")
    elif (MainChoice == 4):
    print("You chose oatmeal.")
    else:
    print("We don't serve that breakfast item!")

    The Above example is a generic example.




  • 0 comments:

    Post a Comment

    Powered by Blogger.

    Tags

    Popular Posts