google.com, pub-4617457846989927, DIRECT, f08c47fec0942fa0 Learn to enjoy every minute of your life.Only I can change my life.: Study testing conditions: if, elif, else in python

Monday, December 16, 2019

Study testing conditions: if, elif, else in python

Testing conditions: if, elif, else :

In Python, we have the if, elif and the else statements for decision making to execute a certain line of codes if a condition is satisfied,and a different set of code in case it is not.

This is the simplest example of a conditional statement. The syntax is:

if(condition):
      indented Statement Block

The block of lines indented the same amount after the colon (:) will be executed whenever the condition is TRUE.


For example, lets say you are recording the score for a certain course.
The total score is 100, with 50 points for the theoretical work and 50 for practical.
You want to display an error message or warning if the score exceeds 100.

score_theory = 40
score_practical = 45
if(score_theory + score_practical > 100):
    print("Invalid score. Please check the input.")
The colon (:) is important because it separates the condition from the statements to be executed after the evaluation of the condition.


The above if statement checks for the 'if' condition and determines the statement
(40 + 45 = 85) > 100 to be FALSE and thus, will not print the warning.
Lets make the statement FALSE and see what happens:

score_theory = 50
score_practical = 55
if(score_theory + score_practical >= 100):
    print("Invalid score. Please check the input.")

Output:
Invalid score. Please check the input.




if-else Statement
 The syntax for the if-else statement is:

if(condition):
      Indented statement block for when condition is TRUE
else:
      Indented statement block for when condition is FALSE

example:
score_theory = 40
score_practical = 45
if(score_theory + score_practical > 100):
    print("Please check the input. Score exceeds total possible score.")
else:
    print("Score validated. Your total is: ", score_theory + score_practical)




Output:

Score validated. Your total is:  85



Multiple tests: if-elif-else Statement:

The syntax followed by the if-else-if statement is:

if(Condition1):
      Indented statement block for Condition1
elif(Condition2):
       Indented statement block for Condition2
else:
       Alternate statement block if all condition check above fails


Example:
coursework = "English"
score_theory = 53
score_practical = 35

if(coursework == "Science" or coursework == "science"):
    if(score_theory > 50):
        print("Please check the input score for 'Science: Theory'.")
    elif(score_practical > 50):
            print("Please check the input score for 'Science: Practical'.") 
    else:
        print("Score validated for Science. Your total is: ",score_theory + score_practical)           
elif(coursework == "English" or coursework == "english"):
    if(score_theory > 60):
        print("Please check the input score for 'English: Theory'.")
    elif(score_practical > 40):
            print("Please check the input score for 'English: Practical'.") 
    else:
        print("Score validated for English. Your total is: ",score_theory + score_practical)
else: print("Coursework not recognized. Please enter score for either Science or English.")

Output:
Score validated for English. Your total is:  88

No comments:

Post a Comment

शिव भोलेनाथ स्तुति

 जय शिवशंकर, जय गंगाधर, करुणा-कर करतार हरे,   जय कैलाशी, जय अविनाशी, सुखराशि, सुख-सार हरे जय शशि-शेखर, जय डमरू-धर जय-जय प्रेमागार हरे,   जय ...