Conditionals in Javascript and Python - Hacks
Apply your skills to basic algorithmic design with conditionals.
π CSP 3.6 Hacks β Conditionals
You will complete three small programs that practice selection using if, if/else, and if/elif/else.
Please do not use nested conditionals as that is for 3.7
How to comeplete
- Read the directions for the hack youβre on.
- Run the starter cell, add your code where marked.
- Test your program with at least the sample inputs provided.
- Make one or more small changes to the starter code (This can be the message, number, range, etc). Please do not just turn in the starter code
- When finished, screenshot or record a short demo and add it to your portfolio.
What weβre assessing
- Correct use of
if,elif,else. - Correct boolean expressions with comparisons and
and/or/not. - Output matches the rules for the given inputs.
- Clean prompts and give some comments that show your understanding.
π’ Hack 1 β Number Range Checker
Goal: Ask for a number and select the correct output.
Rules
- If input is between 0 and 10 (inclusive), print the number then print
"Goodbye". - Else, immediately print
"Goodbye!"only.
Steps
- Prompt:
"Enter a number: "and convert toint. - Write one condition using
>=and<=withand. - Print in the correct order for the true case.
- Print only
"Goodbye!"for the false case.
Sample tests
- Input
5β prints5thenGoodbyeon next line. - Input
-2β printsGoodbye! - Input
10β prints10thenGoodbye
π‘ Hack 2 β Grade Evaluator
Goal: Print messages based on a passing threshold using selection.
Rules
- Ask for a grade
0β100(int). - if the grade is 90 or above, give them an A and tell them to have a good day
- If the grade is 87-90, they have a chance to get an A through the AP test, so tell them βCan be rounded to an Aβ and tell them to have a good day
- If their grade is lower, just tell them to have a good day
Steps
- Prompt:
"Enter your grade (0-100): "and convert toint. - Write the
if/elseusing>=. - Make sure the passing branch prints both lines, in order.
Sample tests to try
100β two lines88β two lines74β one line
Optional
- Add an
elif grade < 0 or grade > 100to warn about invalid input, else keep the same behavior.
π΅ Hack 3 β Access Pass (Hard)
Youβre programming the entrance logic for a concert venue.
Inputs
age(int)has_ticket(string"yes"/"no"β case-insensitive)vip(string"yes"/"no"β case-insensitive)
Rules
- If
vip == "yes"β print"VIP Entrance" - Else if
has_ticket == "yes"andage >= 16β"General Entrance" - Else if
has_ticket == "yes"andage < 16β"Minor Entrance (with guardian)" - Else β
"No Entrance"
Steps
- Read inputs and normalize strings with
.strip().lower(). - Use one
if/elif/elif/elseladder (donβt use nesting). - Combine conditions with
andwhere required. - Print exactly one of the four messages.
Sample tests
age=12, ticket=no, vip=yesβVIP Entranceage=20, ticket=yes, vip=noβGeneral Entranceage=15, ticket=yes, vip=noβMinor Entrance (with guardian)age=20, ticket=no, vip=noβNo Entrance
Optional
- Add an OR: Guests under 8 with a ticket may enter as
"Kid Entrance (with adult)".
Hack 1 β Number Range Checker
Just try your best, we know this can be challenging but please do not use AI to do these problems.
num = int(input("Enter a number: "))
# TODO: Write an if/else that checks if num is between 0 and 10 (inclusive)
# if:
# print(num)
# print("Goodbye")
# else:
# print("Goodbye!")
Hack 2 β Grade Evaluator
Just try your best, we know this can be challenging but please do not use AI to do these problems.
grade = int(input("Enter your grade: "))
# TODO:
# if grade is greater or equal to 90 β print "A", then "have a good day"
# if grade is greater or equal to 87, but less than 90 -> print "can be rounded to an A", then "have a good day"
# if none of these cases apply, print "have a good day"
Hack 3 β Access Pass (Advanced)
Just try your best, we know this can be challenging but please do not use AI to do these problems.
You can always go back to the lesson to relearn the topics. If you are really stuck, just add comments explaining your thought process and where you are stuck on, we will grade it based on your understanding, not just completion.
age = int(input("Age: "))
has_ticket = input("Has ticket? (yes/no): ").strip().lower()
vip = input("VIP? (yes/no): ").strip().lower()
# TODO:
# 1. If vip is "yes", print "VIP Entrance"
# 2. Else if the person has a ticket ("yes") and is 16 or older, print "General Entrance"
# 3. Else if the person has a ticket ("yes") and is younger than 16, print "Minor Entrance (with guardian)"
# 4. Otherwise, print "No Entrance"
Reflection: Think about the following question. Answer in 3-4 sentences
- Did any of the question trip you up? If yes, explain the problem and your solution. If no, give a brief, 1-2 sentence summary of conditionals.
- Can you think of real life examples where we would use conditionals? Please do NOT use one of the hacks as a real life example
- Which operator (
and,or,not) do you think you will use the most, and why?
π§Ύ Turn-in checklist
- All three hacks run and match the rules above them.
- You included at least one changed element for at least one hack, this can be change of message, the range, or the input informationβ¦
- You wrote a 2β3 sentence reflection in your portfolio:
- What conditional form did you use most?
- Where did you use
and/or/not? - What would you like to add to the lesson that would help you better understand the material?