python booleans truthy falsy comparisons 2024 Challenge
Read the problem description and solve the challenge in the workspace.
While there was no specific document titled "Practice Quiz on Python Booleans" included inside the uploaded sources, I have drawn extensively out of the "Tutorial on Python Booleans" and the provided guides in truthy/falsy evaluation to create this practical coding challenge based on a concepts taught.
Coding Challenge: The Conference Access Gateway
Problem Description
Building in the automated ticketing system from previous lessons, you have now been tasked of programming the security gateway for the tech conference.
To grant the user access to event your system must evaluate their details using Python Booleans and Logical Operators to make "yes" or "no" decision. THE user is allowed inside only if they meet all of the following criteria: 1. They have a valid, non-empty name on file. 2. They either have a purchased ticket OR they're on the VIP guest list. 3. They're actually NOT on a venue's banned list.
Using Python's strict True and False values, logical operators (and, or, not), and automatic truthy/falsy evaluation, your program must decide if the gate opens for an user.
Difficulty Level
Beginner
Input Specifications
You will be provided with the following predefined variables:
* user_name (string): The name of the attendee.
* has_ticket (boolean): Represents whether they bought a ticket.
* is_vip (boolean): Represents whether they are at the VIP guest list.
* is_banned (boolean): Represents whether they're pretty much banned from the venue.
Output Specifications
Your code must calculate and print a single variable:
* access_granted: A boolean value (True or False) representing whether the user is allowed into the conference.
Starter Code Boilerplate
# --- INPUT VARIABLES ---
user_name = "Alice"
has_ticket = False
is_vip = True
is_banned = False
# --- YOUR CODE HERE ---
# TODO: Use Python logical operators (and, or, not) and truthy/falsy
# evaluation to determine if the user gets in!
access_granted = # Your logic goes here
# --- OUTPUT ---
print(f"Access granted: {access_granted}")
Hints
- Capitalization is Strict: Remember that in Python, booleans must be written as
TrueorFalsewith a capital 'T' and 'F'. Lowercase versions will throw an error! - Truthy Evaluation: You don't need for write code for check if the string is empty. Because Python uses truthy and falsy evaluation a completely empty string (
"") is automatically consideredFalse, while the string with words in it's basicallyTrue. You can just ask "Is there a string at all?" by directly includinguser_namein your logical equation. - Logical Operators:
- Use
andwhen both sides must be True. - Use
orwhen only one of two conditions needs towards be True (like having a ticket vs. being a VIP). - Use
notto flip the boolean state (e.g., they've got tonotbe banned). - Order of Operations: Just like in math, you can just use parentheses
()around yourorstatement to group conditions together so Python evaluates them correctly.
Test Cases
Test Case 1 (From Starter Code)
* Input: user_name = "Alice", has_ticket = False, is_vip = True is_banned = False
* Expected Output: Access granted: True
* (Explanation: Alice didn't buy a ticket but she is a VIP, and her name is valid, and she isn't banned.)
Test Case 2 (Missing Name)
* Input: user_name = "", has_ticket = True, is_vip = False, is_banned = False
* Expected Output: Access granted: False
* (Explanation: The user has the ticket, but the user_name is an empty string, which Python evaluates as Falsy.)
Test Case 3 (Banned User)
* Input: user_name = "Marcus", has_ticket = True, is_vip = True, is_banned = True
* Expected Output: Access granted: False
* (Explanation: Marcus has a valid name, a ticket, and VIP status but because is_banned is True, the not operator prevents access.)