Complete Python Series · Part 16 of 29
Assignment 03-security-alert
NextGen CTO — Python Assignments
A collection of practical Python exercises based on real-world scenarios from the NextGen CTO learning platform. Each assignment includes starter code, hints, a complete solution, and an explanation.
Exercise: NextGen CTO Course Access Security Alert
Build a simple security alert feature for the NextGen CTO learning platform. Learn to convert string inputs into Boolean values and combine conditions using comparison and logical operators.
Problem statement
NextGen CTO wants to detect unauthorized attempts to access its paid course content. A simplified monitoring system checks whether a course access attempt has been detected, whether the requested course is premium, and whether the user is authorized to access it.
The system should trigger an alert only when an access attempt is detected, the course is premium, and the user is not authorized.
Task requirements
- Capture three inputs using
input(): access attempt detected, premium course, and authorized user. - Each input is provided as the string
TrueorFalse. - Convert each string into a Boolean using the equality operator
==. - Calculate the alert status using the
andandnotlogical operators. - Print the resulting Boolean (
TrueorFalse).
Constraints
- Do not use
ifstatements or loops. - Use comparison and logical operators.
- Assume all inputs are exactly
TrueorFalse(case-sensitive). - Do not use
bool()directly on the input strings.
Good luck trying the exercise!
Starter code
# Inputs are provided as strings "True" or "False"
access_input = input("Access attempt detected (True/False): ")
premium_input = input("Premium course (True/False): ")
auth_input = input("Authorized user (True/False): ")
# TODO: Convert the strings into actual Booleans using comparison
# TODO: Calculate whether a security alert should be triggered
# Rule: Access attempt AND Premium course AND NOT Authorized
# TODO: Print the Boolean result
Expected output
Access attempt detected (True/False): True
Premium course (True/False): True
Authorized user (True/False): False
True
Hints
input()returns a string, even when the user typesTrueorFalse.- Comparing a string with
"True"using==produces a Boolean. - The
andoperator returnsTruewhen all Boolean operands areTrue. - The
notoperator reverses a Boolean value. bool("False")returnsTruebecause the string is non-empty.
Solution: NextGen CTO Course Access Security Alert
The solution uses string comparisons to obtain Boolean values and combines them using logical operators to determine whether an alert should be triggered.
# Capture the three inputs
access_input = input("Access attempt detected (True/False): ")
premium_input = input("Premium course (True/False): ")
auth_input = input("Authorized user (True/False): ")
# Convert strings into Booleans
is_access_attempt = access_input == "True"
is_premium = premium_input == "True"
is_auth = auth_input == "True"
# Calculate the security alert status
should_trigger_alert = is_access_attempt and is_premium and (not is_auth)
# Display the Boolean result
print(should_trigger_alert)
Solution explanation
- Lines 2–4: Capture the three inputs as strings.
- Lines 7–9: Compare each input with
"True"to produce actual Boolean values. - Line 12: Combine the three conditions using
and. Thenot is_authexpression evaluates toTruewhen the user is unauthorized. All three conditions must hold for an alert to trigger. - Line 15: Print the final Boolean result.
Test cases
This exercise simulates alert logic for learning purposes. A production platform must enforce authorization on the server using trusted identity and enrollment data, not user-provided Boolean values.