When designing programs, there are often points where a condition needs to be tested in order to make a decision. Conditions are formed using Boolean logic.
Sometimes we need a program to do something based on the results of two conditions. This simple Python (3.x) program prints out a different message depending on the result of two tests. If each test’s score is greater than or equal to 5, then a message is output saying “You have passed!”. Otherwise, a message is output saying “Sorry, try again.”.
score1 = int(input("What did you score in test 1?"))
score2 = int(input("What did you score in test 2?"))
if score1 >= 5 and score2 >= 5:
print("You have passed!")
else:
print("Sorry, try again.")
This program uses selection to determine whether to print one message or the other:
The program examines the condition of the first Boolean expression score1 >= 5, in line 3.
If the score1 value is greater than or equal to 5, then the first condition is True.
The program then examines the condition of the second Boolean expression score2 >= 5 , in line 3.
If the score2 value is greater than or equal to 5, then the second condition is True.
If both conditions are True , then the combined condition is True. As a result, the program prints “You have passed!”.
If either or both condition is False , then the combined condition is False. As a result, the program prints “Sorry, try again.”.