Python Tutorial, Nested if-else Condition

Nested_if_else_Condition

Program

when you want to divide 2 and 3 both and print some string, In the given program, I used And Operator to satisfy the condition.

Code:


for i in range(1,37):

    if(i%2==0 and i%3==0):

        print("Divide by both 2 & 3,String =",i," Bing_Bang ")

    else:
        if(i%2==0):

            print("String = ",i,"Bing")

        else:
             if(i%3==0 ):

                print("String = ",i,"Bang")

             else:

                print("String= ",i)

    

Using Function

We can also do this program using a function that takes the input from the user side and calculates the result. I declare a function name Number_check which take input from the user side.

Code :


n=int(input("Enter the number="))     #take input from user

Number_check(n)       #calling function

def Number_check(n):

    for i in range(1,n):     #Here 1 is start index of loop and n is end+1 index.

        if(i%2==0 and i%3==0):

            print("Divide by both 2 & 3,String =",i," Bing_Bang ")

        else:
            if(i%2==0):

                print("String = ",i,"Bing")
            else:
                 if(i%3==0 ):

                    print("String = ",i,"Bang")

                 else:

                    print("String= ",i)
        

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top