Python Exercise on Chained conditionals and Nested conditionals

 1) Chained conditionals – conditionals that have 2 or more connected branches. It happens when we need to check keyboard input or compare different items.   

    item1=200.0
    item2=400.0
    item3=600.0
    choice=int(input("What item do you choose?\n 1/2/3?\n "))# I had a problem with output before I checked the type of class "choice", it was "string". Then I converted it to "int"
    if choice==1:
        print ("The price of item1 is ", item1, "dollars")
    elif choice==2:
        print("The price of item2 is ", item2, "dollars")
    elif choice==3:
        print("The price of item3 is ", item3, "dollars")
    else:
        print("You made a wrong choice\n")

2) Nested conditionals are used when one conditional contains one or more other conditionals.

    item1=200.0
    item2=400.0
    item3=600.0
    choice=int(input("What item do you choose?\n 1/2/3?\n "))
    if (choice==1):
        print ("The price of item1 is ", item1, "dollars")
    else:
        if choice==2:
            print("The price of item2 is ", item2, "dollars")
        elif choice==3:
            print("The price of item3 is ", item3, "dollars")
        else:
            print("You made a wrong choice\n")

3) The example with other deeply nested conditionals

item1=200.0
item2=400.0
item3=600.0
def discount_combo1(a,b):
    return (a+b)*0.9
def discount_combo2(a,b,c):
    return (a+b+c)*0.75
choice=int(input("What combo do you want to choose\n 2 (2 items)? 3 (3 items)"))
if choice !=2 and choice!=3: #the first level conditional
    print("You made a wrong choice\n")
else:
    if choice==3: #the second level conditional
        print("The price of combo with item 1, with item 2 and with item 3 is ",  discount_combo2(item1,item2,item3));
    if choice==2:
        a=int(input ("Input tne first item which you want to see in your combo\n", "1/2/3?/n"))
        b= int(input("Input tne second item which you want to see in your combo\n", "1/2/3?/n"))
        if a==1 and b==2: #the third level conditional
            print("The price of combo with item ", a, "and with item ", b, "is ",  discount_combo1(item1,item2))
        elif a==2 and b==3:
            print("The price of combo with item ", a, "and with item ", b, "is ",  discount_combo1(item2,item3))
        elif a==1 and b==3:
            print("The price of combo with item ", a, "and with item ", b, "is ",  discount_combo1(item1,item3))
        else:
            print("You made a wrong choice")

4) In the 4th example, 2 nested conditions, but the code became more structured and much easier to read.

item1=200.0
item2=400.0
item3=600.0
def discount_combo1(a,b):
    return (a+b)*0.9
def discount_combo2(a,b,c):
    return (a+b+c)*0.75
choice=int(input("What combo do you want to choose\n 2 (2 items)? 3 (3 items)"))
if choice==2:
    a=int(input ("Input tne first item which you want to see in your combo\n", "1/2/3?/n"))
    b= int(input("Input tne second item which you want to see in your combo\n", "1/2/3?/n"))
    if(a==1 and b==2):
        print("The price of combo with item ", a, "and with item ", b, "is ",  discount_combo1(item1,item2))
    elif(a==2 and b==3):
        print("The price of combo with item ", a, "and with item ", b, "is ",  discount_combo1(item2,item3))
    elif(a==1 and b==3):
        print("The price of combo with item ", a, "and with item ", b, "is ",  discount_combo1(item1,item3))
    else:
        print("You made a wrong choice")
elif choice==3:
    print("The price of combo with item 1, with item 2 and with item 3 is ",  discount_combo2(item1,item2,item3));
else:
    print("You made a wrong choice\n")
Stephen Olubanji Akinpelu

Previous Post Next Post