Conditional Statements: Multiple Conditions

In the advanced practice of conditional logic, we encounter situations where multiple factors must be evaluated simultaneously.

Logical Operators

Logical operators allow us to combine multiple conditions into a single expression. The three main logical operators in Python are:

Using the and Operator

The and operator allows you to combine two or more conditions that must all be true for the entire expression to be true.

#Example: Using the and operator age = 20 is_student = True if age >= 18 and is_student: print("You are an adult student.")

In this example, the condition age >= 18 and the condition is_student must both be true for the message "You are an adult student." to be printed.

Using the or Operator

The or operator allows you to combine two or more conditions where at least one condition must be true for the entire expression to be true.

#Example: Using the or operator age = 16 has_permission = True if age >= 18 or has_permission: print("You can enter.")

In this example, either the condition age >= 18 or the condition has_permission must be true for the message "You can enter." to be printed.

Using the not Operator

The not operator inverts the truth value of a condition. If the condition is true, not makes it false, and vice versa.

#Example: Using the not operator is_raining = False if not is_raining: print("You can go outside.")

In this example, the condition not is_raining is true because is_raining is false. Therefore, the message "You can go outside." is printed.

Practical Application

In the Labyrinth of Logic, decisions often depend on multiple conditions. Combine conditions using and & or to navigate complex scenarios.

⋆˖⁺‧₊ Create a spell to determine if you can access a restricted area. You need a power level above 60 and a special key (use a bool variable). ₊‧⁺˖⋆

Sample Output:
Your level is 90 and you have a key.
You may access the restricted area.
#Type your code below: