Exercise: Logical expressions

In this exercise, you'll define functions to return Boolean expressions based on input parameters. Before you start coding, remember to check the doctests for examples of what should return True/False. When you think you're done coding, run the tests to make sure they all pass.

Curly Hair Checker

📺 Need help getting started on this one? Watch me go through a similar problem in this video.

Or, if you'd like more guidance in written form, expand this block.
  • Your function only needs a single line of code: one return statement.
  • The return statement should contain a compound Boolean expression using == with the and operator.
  • The Boolean expression should involve the parameters (father_allele, mother_allele) as well as literal strings ("C").
For a code template, expand this block.

Fill in the blanks with the correct comparison and try running the tests.

return father_allele ______ and mother_allele ______
def has_curly_hair(father_allele, mother_allele): """Returns true only if both the father allele and mother allele are "C". >>> has_curly_hair("C", "c") False >>> has_curly_hair("c", "C") False >>> has_curly_hair("s", "s") False >>> has_curly_hair("C", "C") True """ return 0

Presidential Eligibility

If you'd like more guidance, expand this block.
  • Your function only needs a single line of code: one return statement.
  • The return statement should contain a compound Boolean expression using >= with the and operator.
  • The Boolean expression should involve the parameters (age, residency) as well as literal numbers (35, 14).
For a code template, expand this block.

Fill in the blanks with the correct comparison and try running the tests.

return ______ and ______
def can_be_president(age, residency): """ Returns whether someone can be US president based on age and residency. According to the US constitution, a presidential candidate must be at least 35 years old and have been a US resident for at least 14 years. >>> can_be_president(30, 10) False >>> can_be_president(36, 10) False >>> can_be_president(30, 16) False >>> can_be_president(36, 15) True >>> can_be_president(36, 14) True >>> can_be_president(35, 14) True >>> can_be_president(35, 30) True """ return 0 # REPLACE THIS

Seafood Safety

If you'd like more guidance, expand this block.
  • Your function only needs a single line of code: one return statement.
  • The return statement should contain a compound Boolean expression using >=, ==, with the or operator.
  • The Boolean expression should involve the parameters (seafood_type, days_frozen) as well as a literal number (7) and a literal string ("mollusk").
For a code template, expand this block.

Fill in the blanks with the correct comparison and try running the tests.

return ______ or ______
def is_safe_to_eat(seafood_type, days_frozen): """ Returns true if the seafood is safe to eat: either the type is "mollusk" or it's been frozen for at least 7 days. >>> is_safe_to_eat("tuna", 3) False >>> is_safe_to_eat("salmon", 6) False >>> is_safe_to_eat("salmon", 7) True >>> is_safe_to_eat("mollusk", 1) True >>> is_safe_to_eat("mollusk", 9) True """ return 0 # REPLACE THIS

Harvest Time

def harvest_time(month, tuber_size): """ Returns true if it's time to harvest root vegetables: either the month is July or the size of the tuber is at least 2 feet. >>> harvest_time("May", 1) False >>> harvest_time("May", 2) True >>> harvest_time("May", 3) True >>> harvest_time("July", 1) True >>> harvest_time("July", 2) True >>> harvest_time("July", 4) True """ return 0 # REPLACE THIS
➡️ Next up: Conditionals