renegade.py
renegade.py

Reputation: 43

If statement listing exceptions

I'm having an issue with this codebat question:

The parameter weekday is True if it is a weekday, and the parameter vacation is True if we are on vacation. We sleep in if it is not a weekday or we're on vacation. Return True if we sleep in.

sleep_in(False, False) → True sleep_in(True, False) → False sleep_in(False, True) → True

Here's my solution:

 def sleep_in(weekday, vacation):

     if (True, False):
         return False

     else:
         return True

And the result came back as:

sleep_in(False, False) → True   False   X       
sleep_in(True, False) → False   False   OK      
sleep_in(False, True) → True    False   X       
sleep_in(True, True) → True     False   X

I'm confused as to why it's wrong.

In my solution, I stated:

if (True, False):
    return False

And everything being True.

Can any one give me an idea where I am going wrong?

Upvotes: 0

Views: 66

Answers (1)

Makoto
Makoto

Reputation: 106460

We sleep in if it is not a weekday or we're on vacation.

This is best expressed as a straight-up boolean comparison.

def sleep_in(weekday, vacation):
    return not weekday or vacation

Fair warning: this will evaluate to True if either of those parameters are truthy.

Speaking of "truthy", the experssion (True, False) is a tuple, and since it is a non-empty tuple, it will evaluate to True. For reference, here is a list of all expressions that evaluate to False. Anything outside of that list will evaluate to True in a boolean context.

Upvotes: 3

Related Questions