Examples Of If Statement In Python

if statement is a control structure that allows programmers to run codes depend on the condition. If a condition is true, a code will work. If it is not true, another code will work or the program will continue to run other lines.

In this article, we provide you with examples of using if statement in Python. You have to revise these tutorials to be able to understand the examples below.

Before we begin, try to learn these Python basic concepts:

  • To learn decision-making (if-elif-else) in Python, visit our article here.

Example

We write a small program that asks you mathematical answers and you have to answer that question in order to win points and go to the next level. If you failed to answer the first question, the program will bring you back to the first stage or the first question.

Code

#____ProgrammerCodes.com____
while True:
    
    print("_________________________________________________________")
    points = 0
    print("""
              Stage 1:
              --------
              What is the result of (6*18) ?
              Answer this question
          """)
    result = int(input("Result:"))
    if (result == 108):
        points = points + 5    
        print("Correct! (+5 Points)", "Total Points:", points)
        print("____________________________________")
        print("""
              Stage 2:
              --------
              What is the result of (9*80) ?
              Answer this question
          """)
        result = int(input("Result:"))
        if (result == 720):
            points = points + 5
            print("Correct! (+5 Points)", "Total Points:", points)
            print("____________________________________")
            print("""
                  Stage 3:
                  --------
                  What is the result of (12*90) ?
                  Answer this question
              """)
            result = int(input("Result:"))
            if (result == 1080):
                points = points + 5
                print("Correct! (+5 Points)", "Total Points:", points) 
                print("Congratulations! You Have Won The Game!")
                print("Total Points:", points)
                break
            else:
                print("Wrong Answer! Try Again")
                print(points , "Points")
        else:
            print("Wrong Answer! Try Again")
            print(points , "Points")
    else:
        print("Wrong Answer! Try Again")
        print(points , "Points")
#____ProgrammerCodes.com____

Code’s explanation

#____ProgrammerCodes.com____
    print("_________________________________________________________")
    points = 0
    print("""
              Stage 1:
              --------
              What is the result of (6*18) ?
              Answer this question
          """)
    result = int(input("Result:"))
#____ProgrammerCodes.com____

In this part of the code, We displayed the first question and asked the user to answer it then we stored it to a variable in order to deal with it later in the next lines.

#____ProgrammerCodes.com____
    if (result == 108):
        points = points + 5    
        print("Correct! (+5 Points)", "Total Points:", points)
        print("____________________________________")
        print("""
              Stage 2:
              --------
              What is the result of (9*80) ?
              Answer this question
          """)
        result = int(input("Result:"))
        if (result == 720):
            points = points + 5
            print("Correct! (+5 Points)", "Total Points:", points)
            print("____________________________________")
            print("""
                  Stage 3:
                  --------
                  What is the result of (12*90) ?
                  Answer this question
              """)
            result = int(input("Result:"))
            if (result == 1080):
                points = points + 5
                print("Correct! (+5 Points)", "Total Points:", points) 
                print("Congratulations! You Have Won The Game!")
                print("Total Points:", points)
                break
            else:
                print("Wrong Answer! Try Again")
                print(points , "Points")
        else:
            print("Wrong Answer! Try Again")
            print(points , "Points")
    else:
        print("Wrong Answer! Try Again")
        print(points , "Points")
#____ProgrammerCodes.com____

In the first level of the if statement we checked if the user is answered the questions correctly so if it was a correct answer the user will win the points and go to the next stage if not we displayed to the user a message that shows that the answer is wrong and then will go return to the first questions

We did the same thing in the next levels of the if statement but in the last stage when the user will answer all questions correctly we will break the while loop because the game is finished.

Result 1

_________________________________________________________

              Stage 1:
              --------
              What is the result of (6*18) ?
              Answer this question
          
Result:108
Correct! (+5 Points) Total Points: 5
____________________________________

              Stage 2:
              --------
              What is the result of (9*80) ?
              Answer this question
          
Result:

Result 2

____________________________________

              Stage 2:
              --------
              What is the result of (9*80) ?
              Answer this question
          
Result:720
Correct! (+5 Points) Total Points: 10
____________________________________

                  Stage 3:
                  --------
                  What is the result of (12*90) ?
                  Answer this question
              
Result:

Result 3

____________________________________

                  Stage 3:
                  --------
                  What is the result of (12*90) ?
                  Answer this question
              
Result:300
Wrong Answer! Try Again
10 Points
_________________________________________________________

              Stage 1:
              --------
              What is the result of (6*18) ?
              Answer this question
          
Result:

Leave a Comment

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